C# 向MVC4发布复杂数据

C# 向MVC4发布复杂数据,c#,post,asp.net-mvc-4,C#,Post,Asp.net Mvc 4,我是MVC4的新手,我正在尝试让我的控制器接收来自请求的post数据。它是相当大和复杂的。以下是一个片段: 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 这些数据点已发布到页面,但

我是MVC4的新手,我正在尝试让我的控制器接收来自请求的post数据。它是相当大和复杂的。以下是一个片段:

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在点击页面时工作正常,不发送post数据,因此页面工作正常

// the post call?
[HttpPost]
public virtual ActionResult PriceAlert(PostData postdata)
对于模型postData,我将所有元素都设置为字符串或int,对于属性one,我设置了另一个类:

public class customer
{
 ...
 public List<AlertAttribute> attribute { get; set; }
...
}
public class AlertAttribute
{
    public string name { get; set; }
     public string value { get; set; }
}
不确定是否需要,但使用firebug并查看post请求时,内容信息如下:

Content-Length  2313
Content-Type    application/x-www-form-urlencoded
编辑: 为了使这更易于管理,我减少了post值,尝试创建一个简单的post请求

型号:

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&bottomradid=test

Post:

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;}
}

我不完全确定你在问什么。是不是当你发布时,它没有进入预期的发布操作?是因为它没有传递您期望的formcollection数据吗?您可以添加HTML表单的代码(整个代码,或者至少是更高级别的视图)以及该控制器上的操作签名吗?最好定义一个模型类,该模型类是customer类的简化版本。它将使您能够更严格地控制post数据,并且您将能够使用正确的验证属性装饰您的模型。您不必在customer类上执行此操作。只需注意GET方法名为
Alert
,而POST名为
PriceAlert
。除非您使用(Html.BeginForm(“PriceAlert”、“YourControllerName”)在
@中指定
PriceAlert
作为表单帮助程序中的post操作名称{
,它希望post方法名为
Alert
@DoctorJones-我将尝试通过限制我的post值,我们正在从一个经典的ASP设置迁移到一个.net设置,因此这些值已经被定义。但是将限制它们。不,索引不再需要顺序。您必须拥有一个名为Atti的隐藏字段值设置为索引的butes.index。@PeterLaCombJr您不需要有隐藏的索引值,这是另一种方法,但如果没有它,上面的方法可以正常工作。
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

    }
}