开发者

Array Post Binding Asp Net MVC

I'm having some trouble binding arrays on Asp Net MVC, when the object returns in the post method, the arrays properties returns null, here is a sample code that i made simulating the problem:

Any glue is welcome :)

I Have this 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 the 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 i = 0;开发者_如何学Go i < 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 this EditorTemplate

@model TestBinding.Models.Class2
<p>
    @Html.EditorFor(model => model.SampleProperty)
</p>
<p>
    Content</p>
@for (int i = 0; i < Model.SampleStringArray.Length; i++)
{
    @Html.DisplayFor(model => model.SampleStringArray[i]) <br />               
    @Html.HiddenFor(model => model.SampleStringArray[i])
}
<br />

And the Post is returning (i took that from the 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 the model object in the 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜