Asp.Net MVC 3不具有约束力馆藏车型上发布 [英] Asp.Net MVC 3 not binding collections to models on posting
问题描述
我有许多属性目的其中之一是一些其他复杂对象搜索的阵列
像这样
i have a object with many properties of which one is an array of some other complex object
something like this
class obj1
{
public string prop1 {get; set;}
public string prop2 {get; set;}
public obj2[] array {get; set;}
}
class obj2
{
SomeEnum Type{get; set;}
string Content{get; set;}
}
我已经创建了OBJ2阵列编辑模板,可以将其命名为obj2ArrayTemplate结果
这是这样的。
I have created an editor template for the array of obj2 lets name it obj2ArrayTemplate
which is like this
@for (int i = 0; i < Model.Length; i++)
{
@Html.EditorFor(model=>model[i],"obj2Template")
}
和OBJ2编辑模板,可以将它命名为obj2Template结果
这是这样的。
and an editor template for obj2 lets name It obj2Template
which is like this
<div class="editor-label">
@Html.DisplayFor(model=>model.Type,"SomeEnum",ViewData)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
</div>
现在因为财产键入
如果类型 SomeEnum
这是一个枚举
,因此不能由Asp.Net MVC结果直接呈现
我创建了一个模板这也结果
这是这样的结果。
now because property Type
if of type SomeEnum
which is an enum
and hence cannot be rendered directly by the Asp.Net MVC
I have created a Template for this too
which is something like this
<input type="text" value="@Model.ToString()" id="@ViewData.TemplateInfo.HtmlFieldPrefix" name="@ViewData.TemplateInfo.HtmlFieldPrefix"/>
视图被正确的渲染并合并渲染视图的HTML是
the view is being rendered correctly and the HTML of the combined rendered view is
<div class="editor-field">
<input class="text-box single-line" id="array__0__Content" name="array.[0].Content" type="text" value="FBID" />
</div>
<div class="editor-label">
<input type="text" value="Blog" id="array.[1].Type" name="array.[1].Type"/>
</div>
<div class="editor-field">
<input class="text-box single-line" id="array__1__Content" name="array.[1].Content" type="text" value="SOme random blog" />
</div>
<div class="editor-label">
<input type="text" value="Twitter" id="array.[2].Type" name="array.[2].Type"/>
</div>
<div class="editor-field">
<input class="text-box single-line" id="array__2__Content" name="array.[2].Content" type="text" value="Twitter Profile" />
</div>
在IAM回发包含表单这个网站结果
铬是我看这个数据发布
when iam posting back the form containing this html
chrome is showing me this posted data
prop1:val1
prop2:val2
array.[0].Type:Facebook
array.[0].Content:FBID
array.[1].Type:Blog
array.[1].Content:SOme random blog
array.[2].Type:Twitter
array.[2].Content:Twitter Profile
但仍阵列领域这是一个的OBJ2阵列
类型的模式 OBJ1
是无效
我在做什么错了?
推荐答案
能否破解一个:)结果
检查当时正在从我发现有一个服务器发布的请求。(点)在请求额外所以数组不被填充结果
所以不是
Could crack that one :)
inspecting the request that was being posted from the server I found out that there is one .(dot) extra in the request so the array is not being populated
so instead of
array.[0].Type:Facebook
array.[0].Content:FBID
array.[1].Type:Blog
array.[1].Content:SOme random blog
array.[2].Type:Twitter
array.[2].Content:Twitter Profile
这应该被调回
array[0].Type:Facebook
array[0].Content:FBID
array[1].Type:Blog
array[1].Content:SOme random blog
array[2].Type:Twitter
array[2].Content:Twitter
我发现为什么Asp.Net MVC框架中额外的点放的没有明显的原因。当IM样做的一切的MVC方式的结果
所以我改变了我的code,包括一个小的黑客。结果
我说这个喜欢OBJ 2的编辑器模板
and I found no apparent reason of why Asp.Net MVC framework is putting in that extra dot. when im kind of doing everything the MVC way
so I changed my code to include a little hack.
I added this like to the Editor template of obj 2
@{
ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix.Replace(".", "");
}
所以现在一切都运行良好,平稳。
so now everything is running fine and smooth.
这篇关于Asp.Net MVC 3不具有约束力馆藏车型上发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!