繁体   English   中英

无法使用 Jquery 将视图中的数据发送到 controller Asp.net MVC

[英]Unable to send the data from view to controller Asp.net MVC using Jquery

我正在尝试将数据从我的视图发布到 controller,但计数始终为 0。我可以在开发人员控制台中看到数据作为请求的一部分传递,但我的操作方法无法接收它,我无法在这里解决问题。 开发者控制台中的数据: 在此处输入图像描述 来自用户界面的数据:

var massagedRawMaterials = [];

        for (var i = 0; i < 5; i++) {
            massagedRawMaterials.push({
                name: 'Raw Material Name',
                shortCode: 'Short Code',
                price: 'Price'
            });
        }

        //pass json array to controller function to save line items
        $.ajax({
            url: "/Home/Create",
            type: "POST",
            data: JSON.stringify({ rawmaterial: massagedRawMaterials }),
            contentType: "application/json",
            dataType: 'json',
            success: function (savingStatus) {
                },
            error: function (xhr, ajaxOptions, thrownError) {
               }




> Model C#:

    public class MaterialViewModel
    {
    public string Name { get; set; }

    public string ShortCode { get; set; }

    public string Price { get; set; }
    }

Controller method:

 

   public ActionResult Create(List<MaterialViewModel> rawmaterial)
    {
       // some processing
    }

  
    
For me the code looks okay or I am unable to find the issue here.

尝试这个

 $.ajax({
            url: "/Home/Create",
            type: "POST",
            data: { rawmaterial: massagedRawMaterials },
            success: function (savingStatus) {
                       },
            error: function (xhr, ajaxOptions, thrownError) {
                   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM