如何在asp.net mvc控制器中获取发布的表单数据

时间:2014-06-19 19:03:49

标签: c# jquery asp.net-mvc asp.net-mvc-4

我使用$ .ajax方法从视图发布数据。我没有在控制器中发布数据 我尝试过不同的方式来获取数据,但没有成功。 这是我发布的代码

       $.ajax({
            url: "/Groupage/UpdateAddresses",
            type: "POST",
            contentType: "JSON/Application",
            data: JSON.stringify(sr)
        });


[HttpPost]
public void UpdateAddresses(IEnumerable<GroupageAddress> addresses,FormCollection coll)
 {
       //saving data in db
        var list = Request.QueryString.Cast<GroupageAddress>();
        var list1 = Request.QueryString.Cast<IEnumerable<GroupageAddress>>();
      //getting null for addresses parameter
     //Used request

 } 

这里是分组地址类属性

   public class GroupageAddress
    {
    [Key]
    public int Id { get; set; }
    [Required]
    [Display(Name = "Transport Job Id")]
    public int GroupageJobId { get; set; }
    public int TransportJobId { get; set; }
    //Company name is not required
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }
    [Required]
    [Display(Name = "Address1")]
    public string Address1 { get; set; }

    [Display(Name = "Address2")]
    public string Address2 { get; set; }
    [Required]
    [Display(Name = "City")]
    public string City { get; set; }
    [Required]
    [Display(Name = "County")]
    public string County { get; set; }
    [Required]

    [Display(Name = "Post Code")]
    public string PostCode { get; set; }
    [Required]
    [Display(Name = "Country")]
    public int CountryId { get; set; }

    [Required]
    [Display(Name = "Global Transport Job Address Type Id")]
    public int GlobalTransportJobAddressTypeId { get; set; }
    [Display(Name = "Date")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? Date { get; set; }
    [Display(Name = "Time From")]

    public DateTime? TimeFrom { get; set; }
    [Display(Name = "Time To")]

    public DateTime? TimeTo { get; set; }
    [Display(Name = "Actual Time")]

    public DateTime? ActualTime { get; set; }

    public int GroupageAddressSequenceId { get; set; }

    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }
    [ScriptIgnore(ApplyToOverrides = true)]
    public virtual GroupageJob GroupageJob { get; set; }
   }

这里是张贴的表格数据 enter image description here

1 个答案:

答案 0 :(得分:0)

我观察到基于内容类型的mvc会响应数据。 我只使用了$ .post(&#34; / Groupage / UpdateAddresses&#34;,JSON.stringify(sr)),但它发送的内容类型为application / x-www-form-urlencoded;字符集= UTF-8 当我使用下面的那个时 内容类型更改为&#34; Application \ JSON&#34;

  $.ajax({
        url: "/Groupage/UpdateAddresses",
        type: "POST",
        contentType: "Application/JSON",
        data: JSON.stringify(sr)
    });

以C#对象的形式将数据反序列化为方法中的参数。