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

时间:2022-12-04 15:29:14

I am generating a table dynamically in my ASP.NET MVC 4 application. The table is having input boxes in the columns. I have to save the data from these Input boxes. The table generated is 发送从动态表生成的JSON字符串时发送的空数据

我正在我的ASP中动态生成一个表。净MVC 4应用程序。该表在列中有输入框。我必须从这些输入框中保存数据。生成的表

On click of the save button above, I am reading data from table in jquery as

单击上面的save按钮,我正在以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);
                 }
             });

On inspecting 'estimateObject' has the value as
发送从动态表生成的JSON字符串时发送的空数据

在检查“estimateObject”时,其值为

My action method is

我的操作方法是

  [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);
            }
        }

But the 'JsonString' is always null. I tried different things like keeping the string inside a ViewModel in the controller but no help. Kindly help me.

但是JsonString总是空的。我尝试了不同的方法,比如将字符串保存在控制器的ViewModel中,但没有任何帮助。请帮助我。

3 个解决方案

#1


1  

Either try to do this

或者试着这样做

 [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);
            }
        }

or in AJAX call

或者在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);
                 }
             });

#2


1  

I suggest that use from this code

我建议使用这段代码

 public  Class Estimate{
    public int ItemId{get;set;}
    public string Description{get;set;}
    public int Quantity{get;set;}
    public int Amount{get;set;}

    }

And in Controller

和控制器

 [HttpPost]
    public JsonResult SaveEstimate(Estimate model) 
    {
      //To Do
    }

And in Client Code

在客户端代码

      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({ 'model': 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);
                 }
             });

#3


0  

When you send $().ajax your controller action parse by name input HTTP parameters into C# method parameters. To send data in $().ajax used data option

当你发送(美元)。ajax通过名称将控制器动作解析为将HTTP参数输入到c#方法参数中。以$()发送数据。ajax使用数据选择

 $.ajax({
                 type: 'POST',
                 url: '@Url.Action("SaveEstimate")',
                 data: {someData:'sss'}   //Look here
             });

When ajax sended, javaScript object, that you put to data, parsed by member to HTTp parameters (every field of Object put to parameter name to name, value to value).

当ajax发送给数据的javaScript对象时,由成员解析为HTTp参数(对象的每个字段都被放入到参数名称,值到值)。

So in my example there is someData HTTP parameter with value 'sss'. In Action MVC try find HTTP parameters called like C# method parameters and automaticaly set values of theirs.

在我的示例中有一个带值“sss”的数据HTTP参数。在实际操作中,MVC尝试查找HTTP参数,如c#方法参数和它们的自动设置值。

In your code MVC expect parameter JsonString of type String, but $().ajax send a string (after stringify an json) in data, so jQuery create a HTTP parameter data and put a string value to it.

在您的代码MVC中,期望参数JsonString类型为String,但是$()。ajax在数据中发送一个字符串(在stringify json格式之后),因此jQuery创建一个HTTP参数数据并为其设置一个字符串值。

To make your code work you must do the folowing:

要使你的代码工作,你必须做如下的折叠:

Stringify only payload data

Stringify只有有效载荷数据

var estimateObject = { 'JsonString': JSON.stringify(estimDetails) };

Send in ajax your result object simply

将结果对象发送到ajax中

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

Thus, jQuery lookup to estimateObject, find in it only one field JsonObject, create same HTTP parameter and send to server. MVC lookup in HTTP parameters for parameter JsonObject, find it and set value of C# method parameter with name JsonObject value with stringify JSON.

因此,jQuery查找到estimateObject,在其中只找到一个字段JsonObject,创建相同的HTTP参数并发送到服务器。在参数JsonObject的HTTP参数中进行MVC查找,找到它,并使用stringify JSON的名称JsonObject值来设置c#方法参数的值。

#1


1  

Either try to do this

或者试着这样做

 [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);
            }
        }

or in AJAX call

或者在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);
                 }
             });

#2


1  

I suggest that use from this code

我建议使用这段代码

 public  Class Estimate{
    public int ItemId{get;set;}
    public string Description{get;set;}
    public int Quantity{get;set;}
    public int Amount{get;set;}

    }

And in Controller

和控制器

 [HttpPost]
    public JsonResult SaveEstimate(Estimate model) 
    {
      //To Do
    }

And in Client Code

在客户端代码

      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({ 'model': 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);
                 }
             });

#3


0  

When you send $().ajax your controller action parse by name input HTTP parameters into C# method parameters. To send data in $().ajax used data option

当你发送(美元)。ajax通过名称将控制器动作解析为将HTTP参数输入到c#方法参数中。以$()发送数据。ajax使用数据选择

 $.ajax({
                 type: 'POST',
                 url: '@Url.Action("SaveEstimate")',
                 data: {someData:'sss'}   //Look here
             });

When ajax sended, javaScript object, that you put to data, parsed by member to HTTp parameters (every field of Object put to parameter name to name, value to value).

当ajax发送给数据的javaScript对象时,由成员解析为HTTp参数(对象的每个字段都被放入到参数名称,值到值)。

So in my example there is someData HTTP parameter with value 'sss'. In Action MVC try find HTTP parameters called like C# method parameters and automaticaly set values of theirs.

在我的示例中有一个带值“sss”的数据HTTP参数。在实际操作中,MVC尝试查找HTTP参数,如c#方法参数和它们的自动设置值。

In your code MVC expect parameter JsonString of type String, but $().ajax send a string (after stringify an json) in data, so jQuery create a HTTP parameter data and put a string value to it.

在您的代码MVC中,期望参数JsonString类型为String,但是$()。ajax在数据中发送一个字符串(在stringify json格式之后),因此jQuery创建一个HTTP参数数据并为其设置一个字符串值。

To make your code work you must do the folowing:

要使你的代码工作,你必须做如下的折叠:

Stringify only payload data

Stringify只有有效载荷数据

var estimateObject = { 'JsonString': JSON.stringify(estimDetails) };

Send in ajax your result object simply

将结果对象发送到ajax中

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

Thus, jQuery lookup to estimateObject, find in it only one field JsonObject, create same HTTP parameter and send to server. MVC lookup in HTTP parameters for parameter JsonObject, find it and set value of C# method parameter with name JsonObject value with stringify JSON.

因此,jQuery查找到estimateObject,在其中只找到一个字段JsonObject,创建相同的HTTP参数并发送到服务器。在参数JsonObject的HTTP参数中进行MVC查找,找到它,并使用stringify JSON的名称JsonObject值来设置c#方法参数的值。