简体   繁体   English

从Jquery发送数据到控制器。 MVC C#

[英]send Data to controller from Jquery. MVC C#

my jquery script code part , the script is working fine and set data array/object in dataBLL.. 我的jQuery脚本代码部分,脚本运行良好,并在dataBLL中设置了数据数组/对象。

var dataBLL = [];
$('#mytable tr').each(function (i) {

dataBLL.push({

id: $(this).find('td:eq(0)').text(),
ctype: $(this).find('td:eq(1)').text(),
cpath: $(this).find('td:eq(2)').text(),
ckey: $(this).find('td:eq(3)').text(),
ckey: $(this).find('td:eq(4) input:frist').val(),

});
  $.ajax ({
             url:"User/BllBtn",
             type:"POST",
             data:"dataBll="JSON.stringify(dataBLL);
             dataType: "json",
             success: function (e) {

                alert("sucess");
            }})

but i am not able to send this object/Array to my controller to use it's data and iterate through each row entry . 但是我无法将此对象/数组发送到控制器以使用它的数据并遍历每个行条目。

My controller signature 我的控制器签名

[HttpPost]
public ActionResutl BllBtn(List<string> dataBll)
{

}

Please guide how to get this object as list so I can loop into in simplest way. 请指导如何将该对象作为列表获取,以便我可以以最简单的方式进入。

You've got a syntax error (which your browser console will have told you), and also you're producing invalid JSON. 您遇到了语法错误(浏览器控制台将告诉您),并且您正在生成无效的JSON。

data: { "dataBll": dataBLL }

should work I think. 我认为应该可以工作。 Or 要么

data: JSON.stringify({ "dataBll": dataBLL })

at worst. 最坏的情况 Also set 也设置

contentType: 'application/json; charset=UTF-8'

as another option in the ajax call. 作为ajax调用中的另一个选项。

The next problem is you are trying to accept List<string> into your method, but dataBll is a complex object with the following properties: 下一个问题是您尝试将List<string> dataBll方法中,但是dataBll是具有以下属性的复杂对象:

id, ctype, cpath, ckey, ckey

Firstly, you can't define ckey twice in the same object, and secondly your JSON object is incompatible with the type in the C# method. 首先,您不能在同一对象中定义ckey两次,其次,您的JSON对象与C#方法中的类型不兼容。 You need to define a class, eg 您需要定义一个类,例如

public class myNewType
{
  public string id {get; set; }
  public string ctype {get; set; }
  public string cpath {get; set; }
  public string ckey {get; set; }
}

and then accept List<myNewType> as the parameter to the method: 然后接受List<myNewType>作为方法的参数:

public ActionResult BllBtn(List<myNewType> dataBll)

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

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