locked
Display list from model in ASP.Net MVC view where header and details are on the same list RRS feed

  • Question

  • User-1248511554 posted

    I have to display the model in Master-details mode in my partial, but my model is a single entity

    public class Presenter
        {
            public int IdHeader { get; set; } <-- key
            
            public string HeaderName{ get; set; }
    
            public string Mail { get; set; }
    
    
            /*Detail fields*/
    
            public string Note { get; set; }
    
            public int Places { get; set; }
    
            public int? IdIdHeaderInDetail { get; set; } <-- f-key
    
            public int Idetail { get; set; } <-- key
    
            public string DesDetail { get; set; }
    
        }
    
    

    My partial page are:

    @model PagedList.IPagedList<Presenter>
        @using PagedList.Mvc
    
        <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
    
        @{
            Layout = String.Empty;
            var currentModel = string.Empty;
        }
    
    
        @{
            if (Model.Count > 0)
            {
    
    
                foreach (var element in Model)
                {
                    if (currentModel != element .HeaderName)
                    {
                        @Html.DisplayFor(a => Model);
                        while (element .IdIdHeaderInDetail == element .IdHeader)
                        {
                            @Html.DisplayFor(a => element);
                        }
                        if(element .IdEnteStruttura != element .IdEnte)
                        {
                            currentModel = element .HeaderName;
                        }
                    }
                }
            }
            else
            {
                <label class="labels">No records found</label>
            }
    
        }
    
        @{
            if (Model.Count > 0 && Model.PageSize < Model.TotalItemCount)
            {
                <br />
                <div class="pagination">
                    Pagina @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) di @Model.PageCount
    
                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
                </div>
            }
        }
    Doing so, however, triggers a loop and I fear that in the end I will render the entire list on screen n times also because having a single list many data are duplicated
    Friday, October 9, 2020 9:06 AM

All replies

  • User1312693872 posted

    Hi,shaun87Rp

    Doing so, however, triggers a loop

    The following code will lead to loop because of the 'while', you should change the while to if

    while (element .IdIdHeaderInDetail == element .IdHeader)
                        {
                            @Html.DisplayFor(a => element);
                        }

    Best Regards,

    Jerry Cai

    Monday, October 12, 2020 8:46 AM