Issue: Spaces are not being displayed between items in the @Html.DisplayFor statement within the Foreach

I have encountered a few similar queries on this platform and attempted the suggested solutions, but unfortunately, they did not resolve my specific issue.

Within my view, I am using @Html.DisplayFor in a Foreach loop to display all GroupIDs in a particular section. However, instead of displaying them as "1 2 3 4 5", they appear as a single string "12345".

The problem lies in the second row of the table (

@Html.DisplayFor(model => item.Group)
) where there is an issue with the expand/collapse functionality.

View

<table>
     <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td class="col-md-1">+</td>
                <td class="col-md-2">@Html.DisplayFor(model => item.Name)</td>
            </tr>

            <tr>
                <td class="col-md-1" colspan="3"><p style="display:none">@Html.DisplayFor(model => item.Group)</p></td>
            </tr>
        }
     </tbody>
</table>

I have created a simple JSFiddle below for better visualization of the issue at hand. Thank you in advance for your assistance, and feel free to ask for more information if needed. JSFiddle

EDIT Below is the controller code added to demonstrate what 'Group' entails.

public ActionResult SectionTable()
        {
            Manager manager = new Manager();
            var data3 = manager.GetAllSections();
            var groups = manager.GetAllGroups();
            var sectionDetails = from u in data3

                               select new SectionDetail
                               {
                                   SectionID = u.Id,
                                   Name = u.Name,
                                   Description = u.Description,
                                   Group = (from g in groups
                                           where g.SectionId == u.Id
                                           select new GroupDetail() { GroupID = g.Id, GroupDescription = g.Description, GroupName = g.Name, GroupSectionID = g.SectionId, Rights = g.Rights, RightsID = g.RightsId, SectionName = g.SectionName }).ToList()


                               };
            return View(sectionDetails.ToList());
        }

Answer №1

To enhance your layout, consider incorporating a nested foreach loop in the second row to display each Group within a <span> element, as shown below:

<table>
 <tbody>
    @foreach (var item in Model)
    {
      <tr>
        <td class="col-md-1">+</td>
        <td class="col-md-2">@Html.DisplayFor(model => item.Name)</td>
      </tr>
      <tr>
        <td class="col-md-1" colspan="3">
          @foreach(var group in item.Group)
          {
            <span class="group">@group.GroupID</span> // add class name if you want additional spacing
          }
        </td>
      </tr>
    }
  </tbody>
</table>

To adjust the spacing between groups, modify the CSS properties like so:

.group {
  display: inline-block;
  margin: 0, 10px;
}

Additionally, it's recommended to follow standard naming conventions and rename the property Group to Groups given that it represents a collection.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered a Server Error: Invalid hook call. Hooks are restricted to being called within the body of a function component specifically in _app.js

As someone new to React and Next JS, I am facing an issue with setting initial auth user data on the initial load from the __app.js file. Whenever I try to use dispatch, it throws an error saying "Invalid hook call". I understand that calling hooks in the ...

Employing jQuery to locate a class based on its ID designation

<div id="calcwrapper"> <img class="sugarcube" src="images/sugarcube.png" width="13" height="17"> <div id="drinks"> <a id="drink1" href=""><img src="images/drinkicon1.png" width="84" height="81"></a><di ...

What is the best way to extract ABC 12005 from strings like ABC000012005 and ABC0000012005?

My task involves parsing a string with values like ABC000012005,ABC0000012005. The desired output is to extract the prefix and numbers without leading zeros, formatted as ABC 12005, ABC 12005 ...

Is it necessary to store tokens in cookies, local storage, or sessions?

I am currently utilizing React SPA, Express, Express-session, Passport, and JWT. I find myself puzzled by the various client-side storage options available for storing tokens: Cookies, Session, and JWT/Passport. Is it necessary to store tokens in cookies, ...

Retrieve user choices from dropdown menu with Flask and HTML

I'm currently working on developing a dropdown list in HTML that, upon selection submission, will pass the chosen option to a Python script to dynamically update the content of the HTML page. While I have successfully implemented the dropdown list, I ...

Experiencing a problem with my JavaScript code in Node.js due to an asynchronous issue arising when using a timeout

My goal with this code is to retrieve JSON questions and present them to the user through the terminal. The user has 5 seconds to respond to each question before the next one appears. However, I encountered an issue where the next question times out automa ...

What are the steps to creating a duplicate of JotForm?

After exploring JotForm, I discovered it is an extremely interactive form builder utilizing the Prototype JS library. I am curious to know which JS framework or library would be a solid foundation for creating a similar form builder - JQuery, Prototype, ...

Angular 2 - The constructor of a class cannot be called without using 'new' keyword

Currently, I am working on integrating the angular2-odata library into my project. This is the code snippet I have: @Injectable() class MyODataConfig extends ODataConfiguration { baseUrl = "http://localhost:54872/odata/"; } bootst ...

Troubleshooting Problem with Retrieving jQuery Post Outcome

I'm attempting to execute this straightforward Ajax Post example: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"> </script> <script> $(document).ready ...

Create a layout consisting of two rows, each containing three blocks. Ensure that the layout is responsive by using only HTML and CSS, without relying

Hello there! I am looking to create a responsive layout without using bootstrap or any other framework..... I want to build it from scratch. https://i.stack.imgur.com/GAOkh.png I am aiming for responsive blocks that collapse into one column when the page ...

Remove Specific HTML Tags Using Free Pascal

Is it possible to exclude a specific HTML tag from a string? I am looking to remove tags that start with <img>. However, all the content within <img ...> should be deleted as well. This is necessary because I need to eliminate images from the ...

Animation in CSS/transform drifting out of view

I have a challenge with animating text to each corner of the screen, where the text is rotated in every corner. However, I am facing an issue where the text goes off-screen in the top right and bottom left corners. It seems like the rotation might be causi ...

Creating a responsive navigation menu by converting overflowing menu items into a dropdown list

I'm currently designing a menu that contains multiple list items. I would like the overflowing li's to collapse and display in a dropdown when resizing the browser to smaller screens, such as laptops and tablets. Original Menu. https://i.sstatic ...

Capture an entire webpage screenshot with Webdrivercss

When trying to capture a screenshot of an entire webpage, I encountered a challenge. The code I used below with Firefox successfully took a screenshot of the whole page, but it didn't work with Chrome. According to the API documentation, I should use ...

What are some effective tactics for reducers in react and redux?

Working on a React + Redux project to create a web app that communicates with an API, similar to the example provided at https://github.com/reactjs/redux/tree/master/examples/real-world. The API I'm using returns lists of artists, albums, and tracks, ...

The PUT method is not supported when using the $.ajax function with Internet Explorer 11

Currently, on the edit page, an API controller is being utilized. Here is the code block for $.ajax: var settings = {}; settings.baseUri = '@Request.ApplicationPath'; var infoGetUrl = ""; if (settings.baseUri === "/ServerProjectName") { inf ...

Combining two observable entities

Utilizing Angular 4 and rxjs, the objective is to fetch data from two distinct servers in JSON format, merge this data, and exhibit it in a list by associating the list with an observable array. **Word Search Component TypeScript File:** @Component( ...

What could be causing the bootstrap card alignment issue when trying to display them side by side

I am currently developing a Flask project where I'm showcasing user information retrieved from the database within bootstrap cards. Utilizing jinja2 to iterate through an object statuses {% extends "base.html" %} {% block content %} {% if ...

Confirmation dialog with user-defined button text

When the confirm prompt box is used, it typically displays "ok" and "cancel" buttons. I am looking to customize the label text for the buttons to read as Agree and Not Agree instead. If you have any suggestions on how to achieve this modification, please ...

Having trouble with updating PHP MySQL data using serializeArray?

My HTML page includes a display of temperature with two buttons - one for updating MySQL with the temperature setting and the other for toggling on/off data. Previously, everything was functioning correctly with the initial code: function myFunction() { ...