c# - 发送从动态表生成的 JSON 字符串时发布的空数据

标签 c# jquery asp.net-mvc json

我正在 ASP.NET MVC 4 应用程序中动态生成一个表。该表的列中有输入框。我必须保存这些输入框中的数据。生成的表是enter image description here

单击上面的保存按钮后,我将从 jquery 中的表中读取数据

    var estimDetails = $('#editorRows tr:has(td)').map(function (i, v) {
                         var $td = $('td', this);
                         return {
                             ItemId: $($td[0]).find("select").val(),
                             Description: $($td[1]).find("input").val(),
                             Quantity: $($td[2]).find("input").val(),
                             Amount: $($td[3]).find("input").val()
                         }
                     }).get();
    //Convert data to JSON
    var estimateObject = JSON.stringify({ 'JsonString': estimDetails });
//post data to Controller Action
 $.ajax({
                 type: 'POST',
                 url: '@Url.Action("SaveEstimate")',
                 data: estimateObject,
                dataType: 'json',
                 contentType: 'application/json; charset=utf-8',
                 success: function (data) {
                     if (data)
                         alert("Saved Successfully");
                 },
                 error: function (response) {
                     debugger;
                     alert(response);
                 }
             });

检查“estimateObject”的值为
enter image description here

我的操作方法是

  [HttpPost]
        public JsonResult SaveEstimate(string JsonString)
        {
            try
            {
                DateTime expDate;
                DateTime expiryDt = new DateTime();


                return Json("true", JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {

                return Json("false", JsonRequestBehavior.AllowGet);
            }
        }

但是“JsonString”始终为空。我尝试了不同的方法,例如将字符串保留在 Controller 的 ViewModel 内,但没有帮助。请帮助我。

最佳答案

要么尝试这样做

 [HttpPost]
        public JsonResult SaveEstimate(string id) 
        {
            try
            {
                DateTime expDate;
                DateTime expiryDt = new DateTime();


                return Json("true", JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {

                return Json("false", JsonRequestBehavior.AllowGet);
            }
        }

或在 AJAX 调用中

$.ajax({
                 type: 'POST',
                 url: '@Url.Action("SaveEstimate")',
                 data: "JsonString=" + estimateObject,
                 dataType: 'json',                    
                 success: function (data) {
                     if (data)
                         alert("Saved Successfully");
                 },
                 error: function (response) {
                     debugger;
                     alert(response);
                 }
             });

关于c# - 发送从动态表生成的 JSON 字符串时发布的空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474602/

相关文章:

c# - UDP 打洞问题

c# - 无法让 MFC dll 函数在 .net 中运行

c# - 我无法定义 "client"SignalR

javascript - 验证插件同时处理两个表单,错误

javascript - 实时查询插件不适用于可见属性选择器

javascript - 在MVC 3中使用Url.Content加载JS文件

c# - 使用静态表作为动态表的导航属性

c# - .Net Native - 类型不包含在编译中

javascript - 在JavaScript中删除最后一个逗号之前的文本

c# - 除了一个(登录)之外,保护 Controller 所有操作的最佳方法是什么?