locked
How to set OrderBy for Partial views called in some parent? RRS feed

  • Question

  • User991566988 posted

    Hi

    I've called two partial views in a parent view. How to add an OrderByDescending to it?

    The parent model Mobiles:

        public class Mobiles
        {
            public int Id { get; set; }
    
            public string SerialNo { get; set; }
    
            public DateTime ReceiptDate { get; set; }
    
            public DateTime? DeliveryDate { get; set; }
    
            public string Notes { get; set; }
    
            public IList<MobileRuins> MobileRuins { get; set; } = new List<MobileRuins>();
            public IList<MobileMaints> MobileMaints { get; set; } = new List<MobileMaints>();
        }

    The related model MobileRuins:

        public class MobileRuins
        {
            public int Id { get; set; }
            
    public int RuinId { get; set; }
    public int MobileId { get; set; } public string RuinDetails { get; set; } public string Notes { get; set; } public Mobiles Mobile { get; set; }

    public Ruins Ruin { get; set; } }
        public class MobileMaints
        {
            public int Id { get; set; }
    
            public int MobileId { get; set; }
    
            public string MaintDetails { get; set; }
    
            public int CurrencyId { get; set; }
    
            public string Notes { get; set; }
            public bool Done { get; set; }
    
            public Mobiles Mobile { get; set; }
            public Currencies Currency { get; set; }
            
        }

    The get Mobiles\Edit method:

            public async Task<IActionResult> Edit(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                var mobiles = await _context.Mobiles
                    .Include(n => n.MobileRuins)
                    .ThenInclude(m => m.Ruin)
                    .Include(n => n.MobileMaints)
                    .ThenInclude(m => m.Currency)
                    .FirstOrDefaultAsync(n => n.Id == id.Value); 
                if (mobiles == null)
                {
                    return NotFound();
                }
    ViewData["RuinId"] = new SelectList(_context.Ruins, "Id", "Ruin");
    ViewData["CurrencyId"] = new SelectList(_context.Currencies, "Id", "Currency"); return View(mobiles); }

    The Mobiles\Edit view:

    @model Mobile.Models.Mobiles
    
    @{
        ViewData["Title"] = "Edit";
    }
    
    <form asp-action="Edit">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <input type="hidden" asp-for="Id" />
        <div class="container" >
            <div class="row">
                <div class="col-md-4">
                    <label asp-for="SerialNo" class="control-label"></label>
                    <input asp-for="SerialNo" class="form-control" />
                    <span asp-validation-for="SerialNo" class="text-danger"></span>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label asp-for="ReceiptDate" class="control-label"></label>
                        <input asp-for="ReceiptDate" class="form-control" />
                        <span asp-validation-for="ReceiptDate" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label asp-for="DeliveryDate" class="control-label"></label>
                        <input asp-for="DeliveryDate" class="form-control"  />
                        <span asp-validation-for="DeliveryDate" class="text-danger"></span>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label asp-for="Notes" class="control-label"></label>
                        <textarea asp-for="Notes" class="form-control" rows="1"></textarea>
                        <span asp-validation-for="Notes" class="text-danger"></span>
                    </div>
                </div>
            </div>
        </div>
        <br />
    
        <div>
                <ul class="nav nav-tabs">
                    <li class="in active"><a data-toggle="tab" href="#home">Ruins</a></li>
                    <li><a data-toggle="tab" href="#menu1">Maintenance</a></li>
                </ul>
    
                <div class="tab-content">
                    <div id="home" class="tab-pane fade in active">
                        @{ await Html.RenderPartialAsync("~/Views/MobileRuins/ParMobRui.cshtml", Model); }
                    </div>
                    <div id="menu1" class="tab-pane fade">
                        @{ await Html.RenderPartialAsync("~/Views/MobileMaints/ParMobMain.cshtml", Model); }
                    </div>
                </div>
            </div>
       
        <div class="form-group">
            <input type="submit" value="Save" class="btn" />
        </div>
    </form>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
    

    MobileRuins/ParMobRui view:

    @model Mobiles
    
    <table class="table">
        <thead>
            <tr class="success">
                <th>
                    Ruin
                </th>
                <th>
                    Details
                </th>
                <th>
                    Notes
                </th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.MobileRuins.Count; ++i)
            {
            <tr>
                @Html.HiddenFor(m => m.MobileRuins[i].Id)
                @Html.HiddenFor(m => m.MobileRuins[i].MobileId)
                <td width="10%">
                    @Html.DropDownListFor(m => m.MobileRuins[i].RuinId, (SelectList)ViewBag.RuinId)
                </td>
                <td width="45%">
                    @Html.TextBoxFor(m => m.MobileRuins[i].RuinDetails, new { @style = "width:520px" })
                </td>
                <td width="45%">
                    @Html.TextBoxFor(m => m.MobileRuins[i].Notes, new { @style = "width:520px" })
                </td>
            </tr>
            }
        </tbody>
    </table>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
    

    Now this previous one is shown successfully how to orderby its rows descending?

    Wednesday, October 2, 2019 7:31 AM

Answers

  • User-854763662 posted

    Hi musbah7@hotmail.com ,

    Try the following method to sort the data in the partial view in descending order

    var mobiles = await _context.Mobiles
                    .Include(n => n.MobileRuins)
                    .ThenInclude(m => m.Ruin)
                    .Include(n => n.MobileMaints)
                    .ThenInclude(m => m.Currency)
                    .FirstOrDefaultAsync(n => n.Id == id.Value); 

    mobiles.MobileRuins = mobiles.MobileRuins.OrderByDescending(mr=> mr.RuinId).ToList();

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 9:59 AM

All replies

  • User-854763662 posted

    Hi musbah7@hotmail.com ,

    Try the following method to sort the data in the partial view in descending order

    var mobiles = await _context.Mobiles
                    .Include(n => n.MobileRuins)
                    .ThenInclude(m => m.Ruin)
                    .Include(n => n.MobileMaints)
                    .ThenInclude(m => m.Currency)
                    .FirstOrDefaultAsync(n => n.Id == id.Value); 

    mobiles.MobileRuins = mobiles.MobileRuins.OrderByDescending(mr=> mr.RuinId).ToList();

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 9:59 AM
  • User991566988 posted

    You are right

    Thanks

    Sunday, October 6, 2019 5:19 AM