[英]Posting complex data to MVC 4
我是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()
當您點擊頁面而不發送帖子數據時,Get可以正常工作,從而使頁面正常工作。
// 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 = test&BottomAdLink = test&BottomRandID = test
發布:
Attributes[0].name=TriggerValue
Attributes[0].value=451.51
請注意,索引必須從0開始並且是連續的,如果僅發布0,1和5,則將丟失5,因為一旦序列中存在間隔,MVC就會停止綁定列表。
ViewModel:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.