传递一个集合EditorFor()的时候,它会产生输入要素无效名称 [英] when passing a collection to EditorFor(), it generates invalid names for input elements

查看:176
本文介绍了传递一个集合EditorFor()的时候,它会产生输入要素无效名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BookCreateModel它由该书的平面信息,如标题,PublishYear&安培;等加书作者的集合(复杂类型):

I have a BookCreateModel which consists of book's plane info such as Title, PublishYear & etc plus a collection of book Authors (complex type) :

public class BookCreateModel
{
    public string Title { get; set; }
    public int Year { get; set; }
    public IList<AuthorEntryModel> Authors { get; set; }
}

public class AuthorEntryModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

在CreateBook鉴于我已经使用 EditorFor 助手:

in CreateBook view I have used EditorFor helper :

@Html.EditorFor(m => m.Authors, "AuthorSelector")

EDIT1

和AuthorSelector模板如下:

and AuthorSelector template is as below:

<div class="ptr_authors_wrapper">
    @for (int i = 0; i < Model.Count; i++)
    {
       <div class="ptr_author_line" data-line-index="@i">
        @Html.TextBoxFor(o => o[i].FirstName)
        @Html.TextBoxFor(o => o[i].LastName)
       </div>
    }
</div>
<script>
     ...
</script>

AuthorSelector 模板包含哪些需要注意每个渲染项目的索引加上一些javascript其处理孩子输入的交互,需要(内呈现一次的一些包装标记在 AuthorSelector 模板),从而摆脱的循环/或AuthorSelector模板是不可能的。

the AuthorSelector template contains some wrapper markups which need to be aware of each rendered item's index plus some javascript which handle the child input's interactions and need to be rendered once (inside the AuthorSelector template), thus getting rid of the for loop/or the AuthorSelector template is not possible.

现在的问题是EditorFor行为有些奇怪,并产生这样的输入名称:

now the problem is EditorFor act a little strange and generate input names like this :

<input id="Authors__0__FirstName" name="Authors.[0].FirstName" type="text" value="" />
<input id="Authors__0__LastName" name="Authors.[0].LastName" type="text" value="" />

你可以看到生成像作者名称,而不是[0] .FirstName 它增加了一个额外的点,这使得默认的模型绑定无法解析发布的数据。

as you can see instead of generating names like Authors[0].FirstName it adds an extra dot which makes the default model binder unable to parse posted data.

任何想法?

谢谢!

推荐答案

我建议你坚持公约,即替换:

I would recommend you sticking to conventions, i.e. replace:

@Html.EditorFor(m => m.Authors, "AuthorSelector")

@Html.EditorFor(m => m.Authors)

,然后重命名你的〜/查看/共享/ EditorTemplates / AuthorSelector.cshtml 〜/查看/共享/ EditorTemplates / AuthorEntryModel.cshtml 并使它强类型的单个 AuthorEntryModel 模式,摆脱循环的:

and then rename your ~/Views/Shared/EditorTemplates/AuthorSelector.cshtml to ~/Views/Shared/EditorTemplates/AuthorEntryModel.cshtml and make it strongly typed to a single AuthorEntryModel model and get rid of the loop:

@model AuthorEntryModel
@Html.TextBoxFor(o => o.FirstName)
@Html.TextBoxFor(o => o.LastName)

ASP.NET MVC将自动生成的集合中的所有元素编辑模板,生成正确的名称。

ASP.NET MVC will automatically render the editor template for all elements of the collection and generate proper names.

更新:

在这里看到你更新后是我的回应:

After seeing your update here's my response:

在你的主观点:

<div class="ptr_authors_wrapper">
    @Html.EditorFor(m => m.Authors)
</div>

在编辑器中的模板:

@model AuthorEntryModel
<div class="ptr_author_line">
    @Html.TextBoxFor(o => o.FirstName)
    @Html.TextBoxFor(o => o.LastName)
</div>

您会发现其中是完全正常的模板没有脚本。脚本也没什么标记做。他们去到不同的JavaScript文件。在这个文件中,你可以使用jQuery做任何你需要用标记做。它给你的方法,如的.index(),让您获得匹配的选择器元素的索引,这样你就不需要写任何循环,污染事情像数据线索引您的标记属性。

You will notice the absence of script in the template which is perfectly normal. Scripts have nothing to do in markup. They go into separate javascript files. In this file you could use jQuery to do whatever you need to do with your markup. It gives you methods such as .index() that allow you to get the index of the element in the matched selector so that you don't need to write any loops and pollute your markup with things like data-line-index attributes.

这篇关于传递一个集合EditorFor()的时候,它会产生输入要素无效名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆