[英]Posting complex data to MVC 4
I am pretty new to MVC 4 and I am trying to get my controller to recieve the post data from the request. 我是MVC 4的新手,我想让我的控制器从请求中接收发布数据。 It is pretty large and complex. 它很大而且很复杂。 Here is a snippet: 这是一个片段:
Customer.attribute[0].name=TriggerValue
Customer.attribute[0].value=451.51
Firebug shows the url encoded like this: Firebug显示了这样编码的网址:
Customer.attribute%5B0%5D.name=TriggerValue&Customer.attribute%5B0%5D.value=451.51
These data points are posted to to the page, but I am unsure how to get the controller to receive it. 这些数据点已发布到页面,但是我不确定如何让控制器接收它。
I've done the following with no avail: 我没有成功完成以下操作:
// the get call
public virtual ActionResult Alert()
Get works fine when you hit the page w/o sending post data so the page works. 当您点击页面而不发送帖子数据时,Get可以正常工作,从而使页面正常工作。
// the post call?
[HttpPost]
public virtual ActionResult PriceAlert(PostData postdata)
for the model postData I have all the elements as strings or ints, or another class for the attribute one i did this: 对于模型postData,我将所有元素都作为字符串或整数,或者将另一个类用于属性我这样做:
public class customer
{
...
public List<AlertAttribute> attribute { get; set; }
...
}
public class AlertAttribute
{
public string name { get; set; }
public string value { get; set; }
}
I have even tried the below and it also doesn't hit it. 我什至尝试了以下方法,但也没有成功。
[HttpPost]
public ActionResult PriceAlert(FormCollection fc)
Not sure if you need this or not, but when using firebug and looking at the post request the content information is as follows: 不确定是否需要此功能,但是在使用Firebug并查看发布请求时,内容信息如下:
Content-Length 2313
Content-Type application/x-www-form-urlencoded
EDIT: To make this more manageable, I have reduced the post values to try and create a simple posting request. 编辑:为了使它更易于管理,我减少了帖子值以尝试创建一个简单的帖子请求。
Model: 模型:
public class PostData
{
public string BottomAd { get; set; }
public string BottomAdLink { get; set; }
public string BottomRandID { get; set; }
}
Controller: 控制器:
public virtual ActionResult PriceAlert()
{
return View();
}
[HttpPost]
public virtual ActionResult PriceAlert(PostData postdata)
{
return View();
}
[HttpPost]
public ActionResult PriceAlert(FormCollection fc)
{
return View();
}
Post Request: 张贴要求:
BottomAd=test&BottomAdLink=test&BottomRandID=test BottomAd = test&BottomAdLink = test&BottomRandID = test
Post: 发布:
Attributes[0].name=TriggerValue
Attributes[0].value=451.51
Note that indexes must start at 0 and be sequential, if you only post 0,1 and 5, then 5 will be missed because MVC stops binding lists once there is a gap in the sequence. 请注意,索引必须从0开始并且是连续的,如果仅发布0,1和5,则将丢失5,因为一旦序列中存在间隔,MVC就会停止绑定列表。
ViewModel: ViewModel:
public class CustomerVM
{
List<NameValueVM> Attributes {get;set;}
}
public class NameValueVM
{
public string Name {get;set;}
public decimal Value {get;set;}
}
Controller Action: 控制器动作:
public class CustomerController
{
public ActionResult Save(CustomerVM vm)
{
//vm.Attributes should have one item in it
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.