我对MVC 4很新,我试图让我的控制器从请求中接收发布数据。它非常大而且复杂。这是一个片段:
Customer.attribute[0].name=TriggerValue
Customer.attribute[0].value=451.51
Firebug显示如下编码的网址:
Customer.attribute%5B0%5D.name=TriggerValue&Customer.attribute%5B0%5D.value=451.51
这些数据点被发布到页面,但我不确定如何让控制器接收它。
我做了以下但无济于事:
// the get call
public virtual ActionResult Alert()
当你点击页面没有发送帖子数据以使页面有效时,可以正常工作。
// the post call?
[HttpPost]
public virtual ActionResult PriceAlert(PostData postdata)
对于模型postData我将所有元素都作为字符串或整数,或者我为此属性的另一个类:
public class customer
{
...
public List<AlertAttribute> attribute { get; set; }
...
}
public class AlertAttribute
{
public string name { get; set; }
public string value { get; set; }
}
我甚至尝试了下面的内容,它也没有尝试过。
[HttpPost]
public ActionResult PriceAlert(FormCollection fc)
不确定您是否需要,但在使用firebug并查看帖子请求时,内容信息如下:
Content-Length 2313
Content-Type application/x-www-form-urlencoded
编辑: 为了使这更易于管理,我减少了帖子值以尝试创建一个简单的发布请求。
型号:
public class PostData
{
public string BottomAd { get; set; }
public string BottomAdLink { get; set; }
public string BottomRandID { get; set; }
}
控制器:
public virtual ActionResult PriceAlert()
{
return View();
}
[HttpPost]
public virtual ActionResult PriceAlert(PostData postdata)
{
return View();
}
[HttpPost]
public ActionResult PriceAlert(FormCollection fc)
{
return View();
}
张贴申请:
BottomAd =试验&安培; BottomAdLink =试验&安培; BottomRandID =测试
答案 0 :(得分:0)
发表:
Attributes[0].name=TriggerValue
Attributes[0].value=451.51
请注意,索引必须从0开始并且是顺序的,如果只发布0,1和5,那么将丢失5,因为一旦序列中存在间隙,MVC就会停止绑定列表。
视图模型:
public class CustomerVM
{
List<NameValueVM> Attributes {get;set;}
}
public class NameValueVM
{
public string Name {get;set;}
public decimal Value {get;set;}
}
控制器操作:
public class CustomerController
{
public ActionResult Save(CustomerVM vm)
{
//vm.Attributes should have one item in it
}
}