ASP.NET MVC 表单编辑并添加到集合属性
我有一个如下所示的模型:
public class Book
{
public string Name { get; set; }
public IEnumerable<Author> Authors { get; set; }
}
public class Author
{
public string FullName { get; set; }
public DateTime BirthDate { get; set; }
}
我在视图中有一个用于编辑书籍的表单。用于编辑作者集合的部分处于部分视图中。表单字段是使用 Html.EditorFor() 方法生成的。
它非常适合编辑现有数据。我想做的是在作者编辑部分视图中放入多个空白条目,如果用户填写它们,它们将作为新项目添加到作者集合中。
最终视图应如下所示: http://s1.postimage.org/6g9rqfp20/image.jpg
正确的是实现这种行为的方法?
I have a model that looks like this:
public class Book
{
public string Name { get; set; }
public IEnumerable<Author> Authors { get; set; }
}
public class Author
{
public string FullName { get; set; }
public DateTime BirthDate { get; set; }
}
I have a form in a view for editing a book. The section for editing the Authors collection is in a partial view. The form fields are generated with the Html.EditorFor() method.
It works well for editing existing data. What I would like to do is to put in the Authors editing partial view multiple blank entries that if the user fills them they will be added as new items to the Authors collection.
The final view should look something like this:
http://s1.postimage.org/6g9rqfp20/image.jpg
What is the correct way to achieve this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 MVC2,这是您最好的选择
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx#lated-results
If you are using MVC2 this is your best bet
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx#related-results
我不确定您对使用 javascript 库来完成您想要完成的事情有多感兴趣,但这里有一个很好的示例,说明您正在尝试做的事情: 联系人编辑器示例
它使用 knockouts 库,它允许您使用 JavaScript数据绑定。这也为您带来了用户普遍喜欢的网络上良好的厚重应用程序感觉。
如果您仍然对服务器端的工作原理感到好奇,可以查看 Mix11< 中的演示文稿/a>
祝你好运。
I am not sure how interested you are in using javascript libraries to get what you are looking to get done, but here is a great example of what you are trying to do: Contact Editor Example
It uses the knockouts library which allows you to work with JavaScript data binding. This also gives you a nice thick application feel on the web which users generally like.
If you are still curious about how this works with serverside, you can look at this presentation from Mix11
Good luck.