I need to select the last id of a Viewdata

Asked

Viewed 48 times

0

I have a ViewData with my production orders. I’m making a app where an operator can create a new production from the selected production order, but the ideal was that he did not need to automatically select and display the last production order ID.

I’m working with ASP-NET C# razor pages.

This is my Viewdata in the controller:

 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 it’s showing the first Viewdata ID but what I wanted was to show the last one by default.

2 answers

0

The fourth parameter of the constructor SelectList is the value you want to come selected Docs Microsoft.

Try the following:

int ultimoId = 0;
        int.TryParse(_context.Production_Order
                        .OrderByDescending(p => p.production_order_ID)
                        .Select(r => r.production_order_ID)
                        .First().ToString(), out ultimoId);

        ViewData["production_order_ID"] = new SelectList(_context.Production_Order, "production_order_ID", "production_order_ID", ultimoId);
  • Thank you very much for the answer. Yes this would work, but I already solved my problem but I really appreciate the help and anyway.

0


Basically to solve my problem I did

 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 it worked for what it wanted. Thank you all for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.