[英]how to get posted form data in asp.net mvc controller
I have posted data using $.ajax method from view.I am not getting posted data in controller I have tried in different ways to get data but not succeeded. 我在视图中使用$ .ajax方法发布了数据。我没有在控制器中发布数据我试过以不同的方式获取数据但没有成功。 Here is my code posted
这是我发布的代码
$.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
}
here is groupage address class properties 这是分组地址类属性
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; }
}
here is posted form data 这里是张贴的表格数据
I observed that based on content type mvc will respond to data. 我观察到基于内容类型的mvc会响应数据。 I have used just $.post("/Groupage/UpdateAddresses",JSON.stringify(sr)) but it sent content type as application/x-www-form-urlencoded;
我只使用$ .post(“/ Groupage / UpdateAddresses”,JSON.stringify(sr)),但它发送的内容类型为application / x-www-form-urlencoded; charset=UTF-8 When I used the with like below content type is changed to "Application\\JSON"
charset = UTF-8当我使用以下内容时,内容类型更改为“Application \\ JSON”
$.ajax({
url: "/Groupage/UpdateAddresses",
type: "POST",
contentType: "Application/JSON",
data: JSON.stringify(sr)
});
Got the data deserialized in the form of C# object as parameter in method. 以C#对象的形式将数据反序列化为方法中的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.