MVC .NET模型绑定到阵列上飞 [英] MVC .NET Model Binding to Array on the fly

查看:95
本文介绍了MVC .NET模型绑定到阵列上飞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在斯科茨博客<一href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>帖子他介绍了如何发布对象的数组控制器。

In Scotts blog post he describes how to post an array of objects to the controller.

我的提问如何最好地产生这样的观点,即允许用户添加飞?

My Question how best to generate a View for this that allows the user to add more array items on the fly?

如果我写

 foreach(MyModel item in Model)
 {
     <p>@Html.TextBoxFor(m => item.Name)</p>
 }

和有控制器添加一个新的项目到数组每次生成时间&LT;输入类型=文本名称=item.Name/&GT; 失踪在<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow> 1 数组索引。

and have the controller add a new item to the array each time it generates <input type="text" name="item.Name" /> missing the 1 Array index.

如果我的手code中的&LT;输入&GT; 那么它的工作原理,但我失去所有的客户端验证属性,如数据-VAL -required =名称是必需的

If I hand code the <input> then it works but I lose all the client side validation attributes like data-val-required="Name is required"

我希望能够做的是将用户添加新项目动态数组,并仍然保留不显眼的审定?什么是我们的最佳做法?

What I want to be able to do is have the User add new Items to the array on the fly and still retain unobtrusive validation?. What's the best practice for this?

我想我有使用jQuery写我自己,但如果这样我就可以保持验证?

I am thinking I have write it myself using jQuery but if so can I keep the validation?

更新
好像Tassadaque答案是一个很好的.NET解决方案,但貌似很多服务器端的code做东西应该是很容易的。
穆罕默德·阿迪尔扎西答案是确定的,但仍然错过了客户端验证。

Update Seems like Tassadaque answer is a nice .NET solution but looks like a lot of server side code to do something which should be very easy. Muhammad Adeel Zahi answer is ok but still misses out client side validation.

我想我会最终只是手动写我自己的客户端HTML和使用jQuery生活和验证插件。所以我可以做我自己的验证和补充,并没有对服务器进行任何调用的新项目中删除所有客户端。

I think I will end up just writing my own client side HTML manually and using jQuery live and validation plug-in. So I can do all my own validation and adding and removing of new Items all client side without any calls to the server.

推荐答案

您可能会看到<一个href=\"http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style\"相对=nofollow>这在这篇文章史蒂夫验证了其中的文本框可以添加或删除动态可变长度列表

You may see This In this post steve Validated a variable length list in which textboxes may be added or deleted dynamically

这篇关于MVC .NET模型绑定到阵列上飞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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