c# - Using @Html.TextBoxFor with ViewModel -
a quick question:
so, developing small mvc/c# application , using viewmodel pass data view. viewmodel combination of 3 models.
my models:
public class sourcing_profilesourcingareas { public string area { get; set; } public string details { get; set; } } public class defcountry { public string countryname { get; set; } public string countrycode { get; set; } } my viewmodel:
public class sourcingindex { public list<defcountry> countrylst { get; set; } public list<sourcing_profilesourcingareas> arealst { get; set; } } on view, put line @ top @model sourcingindex declare using viewmodel
i able specify model want display using foreach loop, example:
@foreach (sourcing_profilesourcingareas area in model.arealst) { <tr> <td>@area.area</td> <td>@area.details</td> </tr> } and
@foreach (defcountry ctry in model.countrylst) { <option>@ctry.countryname</option> } however, not able create @html.textbox , assign specific property in model!
if possible, want somthing this: @html.textboxfor(model => model.arealst.area)
thank you
it's bit of weird quirk razor. if try , access objects in foreach loop struggles resolve in model is. need use syntax:
@for (int x = 0; x < model.countrylst.count(); x++) { @html.textboxfor(t => t.countrylst[x].countryname) } this should produce input like
<input name="countrylst[1].countryname"/>
Comments
Post a Comment