c# - The post back object is not correctly filled after a submit in MVC4 with editor templates -


i've got troubles mvc4 , editor templates when post after press submit button. model :

public class form {     public form()     {         this.rows = new list<row>();     }      public list<row> rows { get; set; }      public int id { get; set; } }  public class row {     public row()     {         this.label = string.empty;         this.type = string.empty;     }      public string label { get; set; }      public string type { get; set; }      public int id { get; set; } }  public class simplerow : row {     public simplerow()     {         this.value = string.empty;     }      public string value { get; set; } }  public class dropdownrow : row {     public dropdownrow()     {         this.content = new list<contentdropdown>();     }      public list<contentdropdown> content { get; set; } }  public class contentdropdown {     public contentdropdown()     {         this.title = string.empty;         this.selected = false;     }      public string title { get; set; }      public bool selected { get; set; }      public int id { get; set; } } 

this controller :

public class formcontroller : controller {     [authorize]     public actionresult index()     {         return this.view();     }      [httppost]     [authorize]     public actionresult index(httppostedfilebase file)     {         this.viewbag.title = "formulaire collaborateur";          if (file != null && file.contentlength > 0)         {             return this.view(serialisationhelper.deserializefromstream<form>(file.inputstream));         }          return this.view();     }      [httppost]     [authorize]     public actionresult emailform(form updatedform)     {         form f = updatedform; // here, parameter updatedform have first row filled empty strings , other rows null          return this.view("index");     } } 

my index.cshtml :

@model utils.formobject.form @{     viewbag.title = "index";  } @using (html.beginform("emailform", "form", formmethod.post, new { enctype = "multipart/form-data" })) {     <fieldset>         <p>             @for (int = 0; < model.rows.count; i++)             {                 @html.hiddenfor(x => x.id)                 @html.editorfor(x => x.rows[i])                 }         </p>         <p>             <input type="submit" value="save" />         </p>     </fieldset>     <br /> } 

my others views in sub-folder called editortemplates : row :

@model utils.formobject.row @{     viewbag.title = "row"; } @html.hiddenfor(x => x.id) @html.editorfor(x => x) 

simplerow :

@model utils.formobject.simplerow  @{     if (model.type.equals("string"))     {         <br />         @html.hiddenfor(x => x.id)         @html.displayfor(modelitem => modelitem.label)         @html.editorfor(modelitem => modelitem.value)     }     <br/> } 

dropdownrow :

@model utils.formobject.dropdownrow @{     viewbag.title = "dropdownrow"; } @* @html.dropdownlistfor(x => x, new selectlist(model.content, "title", "title"))*@ @{     @html.hiddenfor(x => x.id)     @html.labelfor(item => item.label)     var list = new list<selectlistitem> { new selectlistitem { value = string.empty, text = "--please select--", selected = false } };     foreach (var row in model.content)     {         list.add(new selectlistitem { text = row.title, selected = row.selected });     }     @html.dropdownlistfor(x => x, list) } 

and contentdropdownrow :

@model utils.formobject.contentdropdown @{     viewbag.title = "contentdropdown"; } @html.hiddenfor(x => x.id) @html.labelfor(x => x.title) 

my form approximately displayed (in fact label of dropdown lists displaying 'label' instead of label of property ...). problem when press submit button post value 'updatedform' not filled. first row row[0] have labels filled empty strings , others rows null. tried this didn't managed make works.

i'm little lost here, don't understand why refuse work.

puk

you seem using editor template render row objects don't think you're using correctly. if have editor template single row object need render row collection index.cshtml passing collection template. here's editor template row:

@model utils.formobject.row @{     viewbag.title = "row"; } @html.hiddenfor(x => x.id) @html.editorfor(x => x.label) @html.editorfor(x => x.type) 

render row collection index.cshtml view - no need loop editor template automagically render each row in collection , name them correctly:

@model utils.formobject.form @{     viewbag.title = "index"; } <fieldset>     <p>         @html.hiddenfor(x => x.id)         @html.editorfor(x => x.rows)         </p>     <p>         <input type="submit" value="save" />     </p> </fieldset> 

this should create fields below correctly named , bound part of form object when posted controller:

rows[0].id rows[0].label rows[0].type rows[1].id rows[1].label rows[1].type 

see answer question:

pass values multiple partial views

small write on editor templates here:

codenodes.wordpress.com - mvc3 editor templates


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -