c# - Error selecting Id and Name from dropdownlist selectitems -


i'm developing asp.net mvc application c# , .net framework 4.7.

i have class pass view:

public class createbatchviewmodel {     private readonly list<genericidnametype> lines;      public bool iswizard { get; set; }      public ienumerable<selectlistitem> lineitems     {         { return new selectlist(lines, "id", "name"); }     }      public int productionorderid { get; set; }      public string productionordername { get; set; }      public list<data.batch> batches { get; set; }      public createbatchviewmodel(bool iswizard) : this()     {         iswizard = iswizard;     }      public createbatchviewmodel()     {         lines = new list<genericidnametype>();     }      public createbatchviewmodel(list<data.line> datalines, bool iswizard)     {         iswizard = iswizard;          if (datalines == null)             throw new argumentnullexception("datalines");          lines = new list<genericidnametype>(datalines.count);          genericidnametype generictype = new genericidnametype()         {             id = null,             name = resources.resources.createbatchviewmodeldonthave         };          lines.add(generictype);          foreach (data.line line in datalines)         {             generictype = new genericidnametype()             {                 id = line.lineid.tostring(),                 name = line.name             };              lines.add(generictype);         }     } } 

and use lineitems in view:

@html.dropdownlistfor(m => m.batches[index].lineid, new selectlist(model.lineitems, "id", "name", model.batches[index].lineid)) 

but error message:

system.web.httpexception: 'databinding: 'system.web.mvc.selectlistitem' not contain property name 'id'.'

i don't understand because have id , name (get { return new selectlist(lines, "id", "name"); })

these fields of selectitems: enter image description here

i have tried remove id , name dropdown:

@html.dropdownlistfor(m => m.batches[index].lineid, new selectlist(model.lineitems, model.batches[index].lineid)) 

but shows system.web.mvc.selectlistitem in select.

what doing wrong?

your lineitems property typeof ienumerable<selectlistitem> , selectlistitem contains properties value , text.

change view code

@html.dropdownlistfor(m => m.batches[index].lineid,      new selectlist(model.lineitems, "value", "text", model.batches[index].lineid)) 

or change property ienumerable<line> lineitems , not create selectlist in method , keep existing view code


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -