c# - Passing back a single object when using EditorFor(model[]) -
this has been thorn in side while. if use editorfor on array of objects , editor template has form in ex.
public class foocontroller:controller{ pubic actionresult action(foo foo){ blah... } }
index.cshtml
@model ienumerable<foo> @html.editorfor(m=> m)
editortemplate
@model foo @using (html.beginform("action", "controller")) { @html.textboxfor(f=> f.a) @html.checkboxfor(f=> f.b) @html.labelfor(f=> f.b) }
so i'll hit few problems.
the checkbox label's doesn't bind correctly checkbox (this has label not receiving proper name of property ([0].a
opposed a
).
i'm aware can rid of pre- text doing foreach on model in index screws ids , naming framework doesnt realize there multiples of same item , give them same names.
for checkboxes i've been doing manually such.
@html.checkboxfor(m => m.a, new {id= html.namefor(m => m.a)}) <label for="@html.namefor(m => m.a)">a</label>
however cant solve inability of controller accept item single model. i've tried allowing array of foo's in action
parameters work when first item being edited ([0]...
) if other item in array (ex. [1].a
) controller doesn't know how parse it. appreciated.
make model class properties need. create class in models subfolder
public class mymodel { public ienumerable<foo> foolist { ; set;} public string { get;set;} }
your editorfor have have foreach loop foolist... mvc attempt put model form , return post action in controller.
edit:
you create editortemplate foo. in views/shared/editortemplates folder, create footemplate.cs
@model foo <div class="span6 float-left" style="margin-bottom: 6px"> @html.textboxfor(m => m.a, new { style = "width:190px" }) @html.checkboxfor(m => m.b, new { style = "width:40px" }) @html.validationmessage("foo", null, new { @class = "help-inline" }) </div>
then in view
@foreach (var myfoo in model) { @editorfor(myfoo) }
this still suffers "model gets passed whole" requiredment of yours. not sure why there need process these individually.
Comments
Post a Comment