如何通过JQuery以JSON格式将数组发布到MVC控制器操作 [英] How do I post array to MVC controller action by JQuery in JSON format

查看:72
本文介绍了如何通过JQuery以JSON格式将数组发布到MVC控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在javascript程序中有一些变量。变量如下:



Hi Everyone,

I have some variables in a javascript program. The variables are as follow :

var name = "Kishan";
var marks = 76;
var subjects = new Array();
subjects[0] = new Array("S1","S2", "S3");
subjects[1] = new Array("S4","S5", "S6");





我有一个C语言的模型类#





I have a model class in C#

public class Student
{
   public string Name {get; set;}
   public double Marks {get; set;}
   public List<string[]> Subjects {get; set;}
}





我有一个动作方法在MVC 4控制器类中





I have an action method in MVC 4 controller class

[HttpPost]
public ActionResult(Student model)
{
  return View();
}



我已经在jquery中编写了代码,用于将数据从视图发布到动作




I have written the code in jquery to post the data from view to action

var source = {
                'Name': name,
                'Marks': marks,
                'Subjects': subjects
            }

            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/Admin/Create",
                data: source,
                success: function (data) {
                    alert("Record added successfully.");
                },
                error: function (error) {
                    jsonValue = jQuery.parseJSON(error.responseText);
                    alert("Error : " + jsonValue);
                }
            });







现在的问题是我可以查看名称中的数据并标记变量,但数组(主题)显示为null。



我必须将此数组传递给控制器​​。



请帮忙。



谢谢高级:)




Now the problem is that I can see data in name and marks variables but the array (subjects) in showing null.

I have to pass this array to controller.

Please help.

Thanks in advanced :)

推荐答案

.ajax({
type: POST
dataType: json
url: / Admin / Create
data:source,
success:function(data){
alert( < span class =code-string>记录添加成功。);
},
错误:函数(错误){
jsonValue = jQuery.parseJSON(error.responseText );
alert( 错误: + jsonValue);
}
});
.ajax({ type: "POST", dataType: "json", url: "/Admin/Create", data: source, success: function (data) { alert("Record added successfully."); }, error: function (error) { jsonValue = jQuery.parseJSON(error.responseText); alert("Error : " + jsonValue); } });







现在问题是我可以看到名称中的数据并标记变量,但数组(主题)显示为null。



我必须将此数组传递给控制器​​。



请帮忙。



感谢高级:)




Now the problem is that I can see data in name and marks variables but the array (subjects) in showing null.

I have to pass this array to controller.

Please help.

Thanks in advanced :)


var name = "Kishan";
var marks = 76;
var employees = [
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName": "Jones" }



  var data1= [{ "fname": "Danny", "lname": "LaRusso", "phones":employees}];


.ajax({
url:' @ Url.Action( test,controller)'
数据:JSON.stringify(data1),
类型:' POST'
contentType:' application / json; '
dataType:' json'
su ccess:function(result){
// alert(result.ItemList [0] .Str);
}
});



模型
public class Person
{
public string fname {获得; set ; }
public string lname { get ; set ; }
public 列表< phone>手机{获取; set ; }
}

public class 电话
{
public string firstName { get ; set ; }
public string lastName { get ; set ; }
}

然后是控制器

public ActionResult test(List< person> person)
{

// 返回Json(data,JsonRequestBehavior.AllowGet);
return 查看(人);
}

它应该可以正常工作。
告诉如果 不能正常工作< / person > < / 手机 >
.ajax({ url:'@Url.Action("test", "controller")', data: JSON.stringify(data1), type: 'POST', contentType: 'application/json;', dataType: 'json', success: function (result) { // alert(result.ItemList[0].Str); } }); In model public class Person { public string fname{ get; set; } public string lname{ get; set; } public List<phone> phones { get; set; } } public class Phone { public string firstName { get; set; } public string lastName { get; set; } } and then controller public ActionResult test(List<person> person) { //return Json(data,JsonRequestBehavior.AllowGet); return View(person); } it should work fine. tell if it is not work</person></phone>


这篇关于如何通过JQuery以JSON格式将数组发布到MVC控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆