数组后绑定 Asp Net MVC

发布于 2024-11-06 18:21:08 字数 3582 浏览 0 评论 0原文

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

欢迎使用任何胶水:)

我有这个 2类:

  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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文