html - Why isn't the EditorTemplate showing the data? -
i rewrote page use editortemplates. , nice. however, when postback first row posts data. other 4 rows come null. formsection has list of formrowmodels. , formrows has list of ctrlinputdatamodels. code below.
index
@model inspectionwebformsmvc.viewmodels.formsection @{ viewbag.title = "testing editor templates"; } <h2>test control webforms</h2> @html.editorfor(x => x.sectionrows, "formrowmodel")
editortemplate/formrowmodel
@model inspectionwebformsmvc.viewmodels.formrowmodel @using (html.beginform("index", "testedit", formmethod.post)) { int = 0; //var testctrls = model.where(x => x.rowinput.where(y => types.contains(y.rowctrl.type))); <div style="clear:both; padding:1%;"> <div class="section"> @model.section </div> <div class="number"> @model.sectionnumber </div> <div class="desc"> @model.description </div> <div class="ctrl"> @{ list<string> types = new list<string>() { "dropdown", "text", "radio", "checkbox" } @html.editorfor(y => y.rowinput.where(z => types.contains(z.rowctrl.type)), "ctrlinputdatamodel") } </div>
editortemplate/ctrlinputdatamodel
@model inspectionwebformsmvc.viewmodels.ctrlinputdatamodel @{ var ddv = new selectlist(model.rowctrl.defaultvalues, "value", "label"); switch (model.rowctrl.type) { case "dropdown": //@html.dropdownlistfor(blah => model.inputteddata, ddv) //@html.hiddenfor(blah => model.inputteddataid) //@html.hiddenfor(blah => model.rowctrl.ctrltypeid) @html.dropdownlist("inputteddata", ddv) @html.hidden("inputteddataid", model.inputteddataid) @html.hidden("ctrltypeid", model.rowctrl.ctrltypeid) <br /> break; case "text": //@html.textboxfor(blah => model.inputteddata) //@html.hiddenfor(blah => model.inputteddataid) //@html.hiddenfor(blah => model.rowctrl.ctrltypeid) *@ @html.textbox("inputteddata", model.inputteddata) @html.hidden("inputteddataid", model.inputteddataid) @html.hidden("ctrltypeid", model.rowctrl.ctrltypeid) <br /> break; case "radio": foreach (var sli in ddv) { //@html.labelfor(blah => model.inputteddata, sli.text) //@html.radiobuttonfor(blah => model.inputteddata, sli.value) @html.label("inputteddata", sli.text) @html.radiobutton("inputteddata", model.inputteddata, sli.value) } //@html.hiddenfor(blah => model.inputteddataid) //@html.hiddenfor(blah => model.rowctrl.ctrltypeid) @html.hidden("inputteddataid", model.inputteddataid) @html.hidden("ctrltypeid", model.rowctrl.ctrltypeid) <br /> break; case "checkbox": foreach (var sli in ddv) { //@html.labelfor(blah => model.inputteddata, sli.text) //@html.radiobuttonfor(blah => model.inputteddata, sli.value) @html.label("inputteddataid", sli.text) //@html.checkbox("inputteddata", false) } //@html.hiddenfor(blah => model.inputteddataid) //@html.hiddenfor(blah => model.rowctrl.ctrltypeid) @html.hidden("inputteddataid", model.inputteddataid) @html.hidden("ctrltypeid", model.rowctrl.ctrltypeid) <br /> break; } }
resulting html
<form action="/testedit" method="post"> <div style="clear:both; padding:1%;"> <div class="section"> </div> <div class="number"> 1 </div> <div class="desc"> first row </div> <div class="ctrl"> <select id="sectionrows_0__rowinput_0__inputteddata" name="sectionrows[0].rowinput[0].inputteddata"><option value="1">yes</option> <option value="0">no</option> </select><input data-val="true" data-val-number="the field inputteddataid must number." data-val-required="the inputteddataid field required." id="sectionrows_0__rowinput_0__inputteddataid" name="sectionrows[0].rowinput[0].inputteddataid" type="hidden" value="1" /><input data-val="true" data-val-number="the field ctrltypeid must number." data-val-required="the ctrltypeid field required." id="sectionrows_0__rowinput_0__rowctrl_ctrltypeid" name="sectionrows[0].rowinput[0].rowctrl.ctrltypeid" type="hidden" value="1" /> <br /> </div> </form> <form action="/testedit" method="post"> <div style="clear:both; padding:1%;"> <div class="section"> </div> <div class="number"> 2 </div> <div class="desc"> second row of halls </div> <div class="ctrl"> <select id="sectionrows_1__rowinput_0__inputteddata" name="sectionrows[1].rowinput[0].inputteddata"><option value="1">accept</option> <option value="0">reject</option> </select><input data-val="true" data-val-number="the field inputteddataid must number." data-val-required="the inputteddataid field required." id="sectionrows_1__rowinput_0__inputteddataid" name="sectionrows[1].rowinput[0].inputteddataid" type="hidden" value="2" /><input data-val="true" data-val-number="the field ctrltypeid must number." data-val-required="the ctrltypeid field required." id="sectionrows_1__rowinput_0__rowctrl_ctrltypeid" name="sectionrows[1].rowinput[0].rowctrl.ctrltypeid" type="hidden" value="2" /> <br /> <input id="sectionrows_1__rowinput_1__inputteddata" name="sectionrows[1].rowinput[1].inputteddata" type="text" value="up, up, down, down, left, left, right, right, b, a, b, a, start" /><input data-val="true" data-val-number="the field inputteddataid must number." data-val-required="the inputteddataid field required." id="sectionrows_1__rowinput_1__inputteddataid" name="sectionrows[1].rowinput[1].inputteddataid" type="hidden" value="6" /><input data-val="true" data-val-number="the field ctrltypeid must number." data-val-required="the ctrltypeid field required." id="sectionrows_1__rowinput_1__rowctrl_ctrltypeid" name="sectionrows[1].rowinput[1].rowctrl.ctrltypeid" type="hidden" value="6" /> <br /> </div> </form> <form action="/testedit" method="post"> <div style="clear:both; padding:1%;"> <div class="section"> </div> <div class="number"> 3 </div> <div class="desc"> third row of fits , fixes , polishes </div> <div class="ctrl"> <input id="sectionrows_2__rowinput_0__inputteddata" name="sectionrows[2].rowinput[0].inputteddata" type="text" value="50" /><input data-val="true" data-val-number="the field inputteddataid must number." data-val-required="the inputteddataid field required." id="sectionrows_2__rowinput_0__inputteddataid" name="sectionrows[2].rowinput[0].inputteddataid" type="hidden" value="3" /><input data-val="true" data-val-number="the field ctrltypeid must number." data-val-required="the ctrltypeid field required." id="sectionrows_2__rowinput_0__rowctrl_ctrltypeid" name="sectionrows[2].rowinput[0].rowctrl.ctrltypeid" type="hidden" value="3" /> <br /> </div> </form>
edit:
i've replaced following -
<div class="ctrl"> @{ list<string> types = new list<string>() { "dropdown", "text", "radio", "checkbox" } @html.editorfor(y => y.rowinput.where(z => types.contains(z.rowctrl.type)), "ctrlinputdatamodel") } </div>
with
<div class="ctrl"> @{ //list<string> types = new list<string>() { "dropdown", "text", "radio", "checkbox" }; //var yy = model.rowinput.where(z => types.contains(z.rowctrl.type)); @html.editorfor(y => y.rowinput) } </div>
and
@html.editorfor(x => x.sectionrows, "formrowmodel")
with
@html.editorfor(x => x.sectionrows)
still same result. first row posts data. other rows come null.
so darin's help, got partial view converted editortemplates. which, in case, thing (tm). however, able first row of collection posted back. after googling around, ran across this page (which referenced various stackoverflow questions). after implementing bit of code, nothing posting back. ran across this post on stackoverflow. , saw referenced wrong name collection. needed reference property opposed model name. working should.
a big darin directly , indirectly has provided me (and others). thanks!
for anyone, having bit of trouble getting steve's code work, please @ page. it's overall solution getting custom htmlhelpers work in general.
Comments
Post a Comment