数组后绑定 Asp Net MVC
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论