Array Post绑定Asp Net MVC

时间:2011-05-11 18:25:10

标签: c# .net asp.net-mvc-3 post

我在Asp Net MVC上绑定数组时遇到一些问题,当对象在post方法中返回时,数组属性返回null,这是我模拟问题的示例代码:

欢迎任何胶水:)

我有两个班:

  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; }
  }

这2个动作:

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();
}

观点:

    <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; 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>

使用此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 />

邮报正在回归(我从字典中取出):

{[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]} 

后视图中的模型对象:

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 个答案:

没有答案