asp.net mvc - Cannot use indexing in view because of IEnumerable in Model? -


in model have ienumerable, , due cannot use loop in view. if use foreach html generated doesn't have indexing, need. how solve problem. i'm trying use same view model create , edit , i'm having problem in edit part.

    public class createmodule     {         //empty form handle form serialization         public createmodule()         {         }          public long id { get; set; }         [required]         public string moduleid { get; set; }          [datatype(datatype.datetime)]         public datetime dateentered { get; set; }          public string kindname { get; set; }         public string typename { get; set; }          public string selectedmoduletypename { get; set; }          public ienumerable<selectlistitem> typenames { get; set; }          public ienumerable<property> properties { get; set; }     }     public class property     {         public string name { get; set; }         public string value { get; set; }     } 

here view have used both , foreach, have commented cannot use indexing in currently

  @*   @for (int = 0; < model.properties.count(); i++)   {      <label class="label">@model.properties[i].value</label>      <div class="input-block-level">@html.textboxfor(model => model.properties[i].value, new { @value = model.properties[i].value })</div>    }   *@     @foreach (var properties in model.properties)    {       <div class="label">@properties.name</div>       <div class="input-block-level">@html.textboxfor(model => properties.value, new { @value = properties.value })</div>       <br/>     } 

currently html.textbox generates following 2 names doesn't have indexing in properties.value because of values submitted during post null. if use loop think properties.value change properties[0].value , properties[1].value??? i'm not sure, i'm trying achieve.

if change model

 public ienumerable<property> properties { get; set; } 

to

public list<property> properties { get; set; } 

then following method in repository not work reutrns iqueryable value properties, , casting iqueryable list not possible.

public createmodule getmoduledetails(long id) {      var module = (_dbsis.modules.where(t => t.id == id).select(m => new createmodule       {          id = id,          moduleid = m.moduleid,          typename = m.moduletype.typename,          kindname = m.moduletype.modulekind.kindname,          properties = m.propertyconfiguration.propertyinstances.select(                     x => new property { name = x.property.name, value = x.value })        }));         return (module.firstordefault());   } 

create partial view below, name property.cshtml, , put under views/shared/editortemplates.

@model myapp.models.property  <div class="label">@model.name</div> <div class="input-block-level">@html.textboxfor(model => model.value)</div> <br/> 

then, replace foreach loop in view this:

@editorfor(model => model.properties) 

Comments