0

我对 MVC 4 很陌生,我正试图让我的控制器从请求中接收发布数据。它相当大而且很复杂。这是一个片段:

Customer.attribute[0].name=TriggerValue
Customer.attribute[0].value=451.51

Firebug 显示编码如下的 url:

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 并查看 post 请求时,内容信息如下:

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

4

1 回答 1

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

    }
}
于 2013-06-07T21:57:27.287 回答