c# - Array Post Binding Asp Net MVC -
i'm having trouble binding arrays on asp net mvc, when object returns in post method, arrays properties returns null, here sample code made simulating problem:
any glue welcome :)
i have 2 classes:
public class class1 { public string sampleproperty { get; set; } public class2[] sampleclass2array { get; set; } } public class class2 { public string sampleproperty { get; set; } public string[] samplestringarray { get; set; } }
this 2 actions:
public actionresult sampleaction() { var model = new class1() { sampleproperty = "class 1 property 1 value", sampleclass2array = new class2[] { new class2() { sampleproperty = "position 1 class 2 property 1 value", samplestringarray = "one,two,3,4,5,6".split(new char[]{','}) }, new class2() { sampleproperty = "position 2 class 2 property 1 value", samplestringarray = "seven,8,9,10".split(new char[]{','}) } } }; return view(model); } [httppost] public actionresult sampleaction(class1 model) { dictionary<string, object> parameters = new dictionary<string, object>(); request.params.copyto(parameters); return view(); }
and view:
<legend>class1</legend> <div class="editor-label"> @html.labelfor(model => model.sampleproperty) </div> <div class="editor-field"> @html.editorfor(model => model.sampleproperty) @html.validationmessagefor(model => model.sampleproperty) </div> <div class="editor-field"> @for (int = 0; < model.sampleclass2array.length; i++) { @html.editorfor(model => model.sampleclass2array[i]) <br /> @html.hiddenfor(model => model.sampleclass2array[i]) } </div> <p> <input type="submit" value="create" /> </p>
with editortemplate
@model testbinding.models.class2 <p> @html.editorfor(model => model.sampleproperty) </p> <p> content</p> @for (int = 0; < model.samplestringarray.length; i++) { @html.displayfor(model => model.samplestringarray[i]) <br /> @html.hiddenfor(model => model.samplestringarray[i]) } <br />
and post returning (i took dictionary):
{[sampleproperty, class 1 property 1 value]} {[sampleclass2array[0].sampleproperty, position 1 class 2 property 1 value]} {[sampleclass2array[0].samplestringarray[0], one]} {[sampleclass2array[0].samplestringarray[1], two]} {[sampleclass2array[0].samplestringarray[2], 3]} {[sampleclass2array[0].samplestringarray[3], 4]} {[sampleclass2array[0].samplestringarray[4], 5]} {[sampleclass2array[0].samplestringarray[5], 6]} {[sampleclass2array[0], testbinding.models.class2]} {[sampleclass2array[1].sampleproperty, position 2 class 2 property 1 value]} {[sampleclass2array[1].samplestringarray[0], seven]} {[sampleclass2array[1].samplestringarray[1], 8]} {[sampleclass2array[1].samplestringarray[2], 9]} {[sampleclass2array[1].samplestringarray[3], 10]} {[sampleclass2array[1], testbinding.models.class2]}
and model object in post view:
model {testbinding.models.class1} testbinding.models.class1 sampleclass2array {testbinding.models.class2[2]} testbinding.models.class2[] [0] null testbinding.models.class2 [1] null testbinding.models.class2 sampleproperty "class 1 property 1 value" string
Comments
Post a Comment