简体   繁体   English

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

[英]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.

相关问题 "Asp.net Mvc html 表单发布到错误的控制器操作" - Asp.net Mvc html form is posted to wrong controller action 在ASP.NET MVC中以表单顺序枚举发布的数据 - Enumerating posted data in form order in ASP.NET MVC 如何在ASP.NET MVC控制器中获取发布JSON数据 - How to get the post json data in asp.net mvc controller 获取发布到ASP.NET的表单的ID - Get the ID of a form posted to ASP.NET 如何在asp.net字典中存储所有表单发布的数据 - How to store all the form posted data in asp.net dictionary 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project 动态表单的Asp.NET MVC值已发布但未传递给控制器 - Asp.NET MVC Value of DynamicForm posted but not passed to controller 发生绑定错误时,ASP.NET MVC 3会将发布的表单数据保留在表单中 - ASP.NET MVC 3 keep posted form data in form when there are binding errors C#-ASP.NET MVC从表单到控制器获取数据,我得到一个空对象 - C# - ASP.NET MVC Get data from form to controller, I get an empty object 在ASP.Net MVC控制器中,如何获得一种从JSON和Form-urlEncoded格式识别数据的方法? - In ASP.Net MVC controller, how do I get a method to recognize data from JSON and Form-urlEncoded formats?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM