c# - Model not being passed in post method which should update entities from changed form input values -
my model contains 2 lists of order entity type, differentiating value of status property. when orders created, status property automatically set in progress , in adminorders view, placed in orders in progress table. adminorders view has 2 tables: orders in progress table , orders dispatched table filled model's lists respectively.
when status value of new orders changed drop down list dispatched, , when update button in view clicked, post request should triggered - passing model it. when debug adminorders post method, model parameter null.
here's model class:
public class adminordersviewmodel { public list<order> ordersinprogress { get; set; } public list<order> ordersdespatched { get; set; } } these controller actions:
[httpget] [authorize(roles = "admin")] public actionresult adminorders() { var model = db.orders.where(o => o.status == "in progress").tolist(); var model2 = db.orders.where(o => o.status == "despatched").tolist(); return view(new adminordersviewmodel { ordersinprogress = model, ordersdespatched = model2 }); } [httppost] [authorize(roles = "admin")] public async task<actionresult> adminorders([bind(include = "ordersinprogress, ordersdespatched")] adminordersviewmodel model) { if (modelstate.isvalid) { foreach (var item in model.ordersdespatched) { db.entry(item).state = entitystate.modified; } foreach (var item2 in model.ordersinprogress) { db.entry(item2).state = entitystate.modified; } await db.savechangesasync(); var ordersinprogress = db.orders.where(o => o.status == "in progress").tolist(); var ordersdespatched = db.orders.where(o => o.status == "despatched").tolist(); return view(new adminordersviewmodel { ordersdespatched = ordersdespatched, ordersinprogress = ordersinprogress }); } return view(model); } in get request adminorders method, each model list's entity updated , new model given view when view returned, page should update , orders moved correct table depending on status property. model left null.
the form in view assigns view's model post method model parameter:
@using (html.beginform("adminorders", "manage", new { model = model }, formmethod.post, new { enctype = "multipart/form-data" })) { } i have tried pass in 2 new lists in post method model's lists individually assigned in form request, receive errors. how ensure post request passes updated model adminorders post method?
the full view page code following:
@model valueville.models.adminordersviewmodel @{ viewbag.title = "orders"; } <div class="main-content-container"> <h1>new orders in progress</h1> @using (html.beginform("adminorders", "manage", new { model = model }, formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() <table class="panel panel-default table cart-table"> <tr> <th> order id </th> <th> total </th> <th> date </th> <th> status </th> </tr> @foreach (var item in model.ordersinprogress) { <tr> <td> <a href="@url.action("orderdetails", "home", new { id = item.orderid })"> @item.orderid </a> </td> <td> £@item.total </td> <td> @item.orderdate </td> <td> @{ list<selectlistitem> listitems = new list<selectlistitem>(); listitems.add(new selectlistitem { text = item.status, value = "option1" }); listitems.add(new selectlistitem { text = "despatched", value = "option2", }); } @html.dropdownlistfor(m => item.status, listitems, item.status) </td> </tr> } </table> <h1>orders despatched</h1> <table class="panel panel-default table cart-table"> <tr> <th> order id </th> <th> total </th> <th> date </th> <th> status </th> </tr> @foreach (var item in model.ordersdespatched) { <tr> <td> <a href="@url.action("orderdetails", "home", new { id = item.orderid })"> @item.orderid </a> </td> <td> £@item.total </td> <td> @item.orderdate </td> <td> @{ list<selectlistitem> listitems = new list<selectlistitem>(); listitems.add(new selectlistitem { text = item.status, value = "option1" }); listitems.add(new selectlistitem { text = "despatched", value = "option2", }); } @html.dropdownlistfor(m => item.status, listitems) </td> </tr> } </table> <div class="panel-body form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="update" class="btn btn-success" /> </div> </div> } </div>
modify viewmodel to:
public class adminordersviewmodel { public list<order> ordersinprogress { get; set; } public list<order> ordersdespatched { get; set; } public list<status> orderstatuses { get; set; } } your view should like:
@model valueville.models.adminordersviewmodel @{ viewbag.title = "orders"; } <div class="main-content-container"> <h1>new orders in progress</h1> @using (html.beginform("adminorders", "manage", formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() <table class="panel panel-default table cart-table"> <thead> <tr> <th>order id</th> <th>total</th> <th>date</th> <th>status</th> </tr> </thead> <tbody> @for (int = 0; < model.ordersinprogress.count(); i++) { <tr> <td> <a href="@url.action("orderdetails", "home", new { id = model.ordersinprogress[i].orderid })">@model.ordersinprogress[i].orderid</a> @html.hiddenfor(x => x.model.ordersinprogress[i].orderid) </td> <td> £@model.ordersinprogress[i].total @html.hiddenfor(x => x.model.ordersinprogress[i].total) </td> <td> @model.ordersinprogress[i].orderdate @html.hiddenfor(x => x.model.ordersinprogress[i].orderdate) </td> <td> @html.dropdownlistfor(m => model.ordersinprogress[i].status, new selectlist(model.orderstatuses, "id", "statusname")) </td> </tr> } </tbody> </table> <h1>orders despatched</h1> <table class="panel panel-default table cart-table"> <thead> <tr> <th>order id</th> <th>total</th> <th>date</th> <th>status</th> </tr> </thead> <tbody> @for (int = 0; < model.ordersdespatched.count(); i++) { <tr> <td> <a href="@url.action("orderdetails", "home", new { id = model.ordersdespatched[i].orderid })">@model.ordersdespatched[i].orderid</a> @html.hiddenfor(x => x.model.ordersdespatched[i].orderid) </td> <td> £@model.ordersdespatched[i].total @html.hiddenfor(x => x.model.ordersdespatched[i].total) </td> <td> @model.ordersdespatched[i].orderdate @html.hiddenfor(x => x.model.ordersdespatched[i].orderdate) </td> <td> @html.dropdownlistfor(m => model.ordersdespatched[i].status, new selectlist(model.orderstatuses, "id", "statusname")) </td> </tr> } </tbody> </table> <div class="panel-body form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="update" class="btn btn-success" /> </div> </div> } </div> the controller method should like:
[httppost] public async task<actionresult> adminorders(adminordersviewmodel model) { // stuff } assuming status model somethig like:
public class status { public int id { get; set; } public string statusname { get; set; } } you can create dropdownlist above list:
@html.dropdownlistfor(m => model.ordersdespatched[i].status, new selectlist(model.orderstatuses, "id", "statusname")) ps: make sure assign list of statuses in method.
Comments
Post a Comment