c# - Error when using a DropDownListFor in a foreach loop in an MVC Razor view -
i trying create simple form in asp.mvc shows list of quotes, , allows user create work orders selecting person drop downwards list assign quote to, , submitting form in 1 go.
my problem utilize of dropdownlistfor within foreach loop causing error , don't think i'm going right way.
a (somewhat simplified) quote looks this:
public class quoteitem { public quoteitem() { this.isworkorder = false; } [key] public int quoteitemcostid { get; set; } public string item { get; set; } public decimal? subtotal { get; set; } public bool isworkorder { get; set; } [notmapped] public string assignedto { get; set; } } i have view model configured follows:
public class unassignedworkordersviewmodel { public ienumerable<quoteitemcost> quoteitemcosts { get; set; } public ienumerable<selectlistitem> trades { get; set; } } and controller action looks this:
public actionresult unassigned(int id = 0) { var trades = db.userprofiles.where(u => u.typpe == "trade").toarray().select(x => new selectlistitem { value = x.firstname + " " + x.lastname, text = x.firstname + " " + x.lastname }).tolist(); var quoteitems = db.quoteitems .where(x => x.isworkorder == false) .tolist(); unassignedworkordersviewmodel viewmodel = new unassignedworkordersviewmodel { trades = trades, quoteitemcosts = quoteitemcosts }; homecoming view(viewmodel); } my problem in view, i'm trying show each work order , drop downwards list of trades next it, , way thought i'd map class property "assignedto" value in drop downwards list, doesn't work, dropdownlist expects linq look first parameter:
@model project.viewmodels.unassignedworkordersviewmodel @if (model.quoteitemcosts.any()) { using (html.beginform("unassigned", "workorder", formmethod.post, null)) { @html.antiforgerytoken() <table> @foreach (var item in model.quoteitemcosts) { <tr> <td style="width:100px;"> <b>item:</b> @item.item </td> <td style="width:200px;"> <b>total:</b> @item.subtotal </td> <td> <b>assign:</b> @html.dropdownlistfor( item.assignedto, // line causes error model.trades, "", new { @id = "ddlstatus", @style = "width:100%" }) </td> </tr> } </table> } how can resolve , going right way? on form submit, should expect viewmodel homecoming quoteitems, assignedto entry if user has selected person drop downwards list.
edit:
andrei's suggestion below has helped , led me solution dropdownlistfor looks this:
@html.dropdownlistfor( m => m.quoteitemcosts.first(q => q.quoteitemcostid == item.quoteitemcostid).assignedto, model.trades, "", new { @id = "ddlstatus", @style = "width:100%" }) but when submit form viewmodel null:
[httppost] [validateantiforgerytoken] public actionresult unassigned(unassignedworkordersviewmodel viewmodel) { // viewmodel null homecoming redirecttoaction("index"); } i've updated razor code include html.beginform well.
to correct, need create utilize of regular for loop here:
@for (int = 0; < model.quoteitemcosts.count; i++) { <tr> <td style="width:100px;"> <b>item:</b> @model.quoteitemcosts[i].item </td> <td style="width:200px;"> <b>total:</b> @model.quoteitemcosts[i].subtotal </td> <td> <b>assign:</b> @html.dropdownlistfor( m => m.quoteitemcosts[i].assignedto, // line causes error model.trades, "", new { @id = "ddlstatus", @style = "width:100%" }) </td> </tr> } now, might wander why not maintain foreach , utilize m => item.assignedto. known problem foreach loop in asp.net mvc views. issue here look above generate select tag wrong name attribute, , won't receive value when form posted. name generated regular loop correct.
c# asp.net asp.net-mvc razor
No comments:
Post a Comment