简体   繁体   English

在MVC中发送JSON数据

[英]Sending JSON Data in MVC

Currently am making a jQuery ajax call to MVC method and sending data from Controller in the below format: 当前,我正在对MVC方法进行jQuery ajax调用,并以以下格式从Controller发送数据:

["UserInfo ID","User ID"]

Controller code: 控制器代码:

     var autoSuggestlist;
      ........
      .
     return Json(autoSuggestlist, JsonRequestBehavior.AllowGet);

Now I want to add another different data like: 现在,我想添加另一个不同的数据,例如:

[ {"editable":true,"edittype":"integer","index":"userInfoId" ]

How I can send these 2 different datas in Controller to jQuery Ajax 我如何在Controller中将这2种不同的数据发送到jQuery Ajax

In the below code 在下面的代码中

$.ajax(
        {
            type: "GET",
            url: "/Home/GetColumnNamesForGrid",
            data: "",
            dataType: "json",
            async: false,
            success: function (result) {

result should get me both the above JSON data. 结果应该让我得到以上两个JSON数据。 How do I need to modify my Controller Code. 我该如何修改我的控制器代码。 Please assist 请协助

Thanks 谢谢

Not sure exactly what you mean, but if you want to sent an object with properties from the controller, you can do this: 不确定确切含义,但是如果要从控制器发送具有属性的对象,则可以执行以下操作:

return Json(new { editable = true, edittype = "integer", index = "userInfoId" }, JsonRequestBehavior.AllowGet);

Then from javascript, your result object can be used as follows: 然后从javascript中,您的result对象可以按如下方式使用:

var editable = result.editable;//will be true

If you are actually wanting to send both data types back at the same time, then create a wrapper object like so: 如果您实际上想同时发送两种数据类型,请创建一个包装对象,如下所示:

var myObject = new { editable = true, edittype = "integer", index = "userInfoId" };
var myArray = autoSuggestlist;

return Json(new { @myObject = myObject, @myArray = myArray}, JsonRequestBehavior.AllowGet);

Then use in javascript like this: 然后像这样在javascript中使用:

var myObject = result.myObject;
var editable = myObject.editable;//will be true

var myArray = result.myArray;
var firstItem = myArray[0];//will be "UserInfo ID"

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

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