将 formCollection 数组转换为控制器中的对象
在我看来,我有几个 [n].propertyName 数组字段,当它进入控制器时,我想将 formCollection 字段转换为对象 myobject[n].propertyName 。
例如,上下文:
View:
foreach (var item in Model.SSSubjobs.AsEnumerable())
<%: Html.Hidden("["+c+"].sssj_id", item.sssj_id ) %>
<%: Html.Hidden("["+c+"].order_id", item.order_id ) %>
<%: Html.TextBox("["+c+"].farm", item.farm %>
<%: Html.TextBox("["+c+"].field", item.field %>
c++;
Controller:
我想将上面的 [0].sssj_id 变成 sssj[0].sssj_id 或 sssj 对象列表
我的第一个想法是在表单集合中查找以“[”开头的内容,但我有一种感觉这是不对的......
这是我得到的:
public IList<SoilSamplingSubJob> extractSSSJ(FormCollection c)
{
IList<SoilSamplingSubJob> sssj_list=null;
SoilSamplingSubJob sssj;
var n=0;
foreach (var key in c.AllKeys) // iterate through the formcollection
{
var value = c[key];
if(key.StartsWith("[")) // ie turn [0].gps_pk_chx into sssj.gps_pk_chx
???
}
return sssj_list;
}
in my view I have several [n].propertyName array fields I want to turn the formCollection fields into objects myobject[n].propertyName when it goes to the controller.
so for example, the context:
View:
foreach (var item in Model.SSSubjobs.AsEnumerable())
<%: Html.Hidden("["+c+"].sssj_id", item.sssj_id ) %>
<%: Html.Hidden("["+c+"].order_id", item.order_id ) %>
<%: Html.TextBox("["+c+"].farm", item.farm %>
<%: Html.TextBox("["+c+"].field", item.field %>
c++;
Controller:
I want to take the above [0].sssj_id and turn into sssj[0].sssj_id or a list of sssj objects
My first idea was to look in the form collection for things starting with "[" but I have a feeling this isnt right...
this is as far as I got:
public IList<SoilSamplingSubJob> extractSSSJ(FormCollection c)
{
IList<SoilSamplingSubJob> sssj_list=null;
SoilSamplingSubJob sssj;
var n=0;
foreach (var key in c.AllKeys) // iterate through the formcollection
{
var value = c[key];
if(key.StartsWith("[")) // ie turn [0].gps_pk_chx into sssj.gps_pk_chx
???
}
return sssj_list;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会让模型框架为你做这件事,而不是你自己编写代码。从您的代码中我看不出您不想这样做的任何原因。
查看 Phil Haack 关于 模型绑定 的帖子到列表。
I would let the model framework do this for you instead of writing the code yourself. From your code I cant see any reason why you would not want to do this.
Have a look at Phil Haack's post on model binding to a list.