通过控制器中的FormCollection和一个IList< T> [英] Passing Controller a FormCollection and an IList<T>

查看:106
本文介绍了通过控制器中的FormCollection和一个IList< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含将由用户包括一些元素,用户可以决定有多少,他们将进入同一项目的完成数据输入字段的整个堆的形式。这是在菲尔哈克的博客文章正在使用模型绑定到一个列表

我成功地使用jQuery创建额外的表单元素

,索引正确,等我的问题是实际上我的控制器内读取这些的最佳途径。在文章中的控制器只希望有一个对象,的IList<产品> 而我的控制器已经需要一个的FormCollection形式现在我想也发送一个的IList< Itmes方式> 以及

我应该加入这个由控制器预期的参数或通过表单访问[项目]还是别的什么?

查看

 <形式的行动=/ MyItems /添加方法=后>
   <输入类型=文本名称=标题值=/>   <输入类型=隐藏的名字=myItem.Index值=0/>
   <输入ID =项目[0] .AmountNAME =项目[0] .Amount类型=文本值=/>
   <输入ID =项目[0] .Name点NAME =项目[0] .Name点类型=文本值=/>   <输入类型=隐藏的名字=myItem.Index值=1/>
   <输入ID =项目[1] .AmountNAME =项目[1] .Amount类型=文本值=/>
   <输入ID =项目[1] .Name点NAME =项目[1] .Name点类型=文本值=/>
< /表及GT;

控制器

 公众的ActionResult添加(的FormCollection形式)
{
    字符串标题=形式[标题];
    清单<项目>项目=形式[条]了ToList()。
}

DTO

 公共类项目()
{
    INT金额{搞定;组; };
    字符串名称{;组; };
}


解决方案

我已经决定与专门与的FormCollection 而工作比弄脏了一些数据水域传递通过使用的FormCollection 和其他数据被映射到由框架列表。下面code需要的物品和手工滋润DTO。这很好地工作,让我对我的code,它是不可能直接映射到一个列表中做一些其他的事情。

 列表<项目> ITEMLIST =新的List<项目>();
INT I = 0;而(表格[项目[+ I +] .Amount]!= NULL)
{
    itemList.Add(新项目()
        {
            金额= Convert.ToInt32(形式[的String.Format(项[{0}]。金额,I)]),
            名称=形式[的String.Format(项[{0}]名,I)]
        });
}

I have a form which contains a whole heap of data entry fields that will be completed by the user including some elements where the user can dictate how many of the same items they will enter. This is as is being used in Phil Haack's blog entry Model Binding To A List.

I am successfully using JQuery to create the extra form elements, correctly indexed, etc. My issue is the best way to actually read these within my Controller. The Controller in the article only expects one object, IList<Product> whereas my Controller already expects a FormCollection form and now I am trying to also send it an IList<Itmes> as well.

Should I be adding this to the parameters expected by the Controller or accessing via form["items"] or something else?

View

<form action="/MyItems/Add" method="post">
   <input type="text" name="Title" value="" />

   <input type="hidden" name="myItem.Index" value="0" />
   <input id="item[0].Amount" name="item[0].Amount" type="text" value="" />
   <input id="item[0].Name" name="item[0].Name" type="text" value="" />

   <input type="hidden" name="myItem.Index" value="1" />
   <input id="item[1].Amount" name="item[1].Amount" type="text" value="" />
   <input id="item[1].Name" name="item[1].Name" type="text" value="" />
</form>

Controller

public ActionResult Add(FormCollection form)
{
    string Title = form["Title"];
    List<Item> Items = form["items"].ToList();
}

DTO

public class Item()
{
    int Amount {get; set; };
    string Name {get; set; };
}

解决方案

I have decided to work with exclusively with the FormCollection rather than muddying the waters with some data being passed through using FormCollection and other data being mapped to a List by the framework. The below code takes the items and hydrates the DTO manually. This works nicely and allows me to do some other things within my code that were not possible mapping directly to a List.

List<Item> itemList = new List<Item>();
int i = 0;

while ( form["item[" + i + "].Amount"] != null)
{
    itemList.Add(new Item()
        {
            Amount = Convert.ToInt32(form[String.Format("item[{0}].Amount",i )]),
            Name = form[String.Format("item[{0}].Name",i )]
        });
}

这篇关于通过控制器中的FormCollection和一个IList&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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