Search code examples
c#asp.netviewbagviewdata

Need to select last ID from a viewdata


I have an ViewData with my production orders in it. Im doing a thing where the operator can create a new production by selecting the production order he wants to work on, but the ideal was to show already the last production order created, instead of him having to go across all the options and select the last one. Can someone help me out?

I'm working with ASP-NET C# Razor Pages.

This is my ViewData

 ViewData["production_order_ID"] = new SelectList(_context.Production_Order, "production_order_ID", "production_order_ID");

This is my page

<div class="col-md-2" style="margin-top:10px">
                <label asp-for="Production.production_order_ID" class="control-label">Ordem de Produção:</label>
                <select id="orderList" name="ids" asp-for="Production.production_order_ID" class="form-control" asp-items="ViewBag.production_order_ID" onchange="getSensors(); myFunction(event);"></select>
            </div>

Now is showing the first item on the ViewData but i need to show the last one by default.


Solution

  • So basically what i just did was

    ViewData["production_order_ID"] = new SelectList(_context.Production_Order.Where(c => c.User.AspNetUser.UserName.Equals(User.Identity.Name)).OrderByDescending(c => c.production_order_ID).ToList(), "production_order_ID", "production_order_ID");

    and worked just fine for what i intended to. Sorry for wasting your time guys, but thank you so much for your answer.