最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send multiple JSON results from controller to view? - Stack Overflow

programmeradmin2浏览0评论

Here I am sending one item from controller to view. How can I send more than one item from controller to view??

  //Controller 

  public JsonResult GetQuantity(string id)
    {
       string getQuantity = "";
       string items="Hello Quantity"
       getQuantity = items.ToString(CultureInfo.InvariantCulture);
       return Json(getQuantity, JsonRequestBehavior.AllowGet);
    }


        // View

      function GetQuantity(id) {


            var targetDiv = '#divYarnDistributionViewer';
            var url = "/STSM/STSA/YarnDistribution/GetQuantity/" + id;
            var form = $("#frmYarnDistributionViewer");

            var serializedForm = form.serialize();

            $.post(url, serializedForm, function (result) {
                $("#YarnDistribution_StockQuantity").val(result);

            }, "json");

        return false;


    }

Here I am sending one item from controller to view. How can I send more than one item from controller to view??

  //Controller 

  public JsonResult GetQuantity(string id)
    {
       string getQuantity = "";
       string items="Hello Quantity"
       getQuantity = items.ToString(CultureInfo.InvariantCulture);
       return Json(getQuantity, JsonRequestBehavior.AllowGet);
    }


        // View

      function GetQuantity(id) {


            var targetDiv = '#divYarnDistributionViewer';
            var url = "/STSM/STSA/YarnDistribution/GetQuantity/" + id;
            var form = $("#frmYarnDistributionViewer");

            var serializedForm = form.serialize();

            $.post(url, serializedForm, function (result) {
                $("#YarnDistribution_StockQuantity").val(result);

            }, "json");

        return false;


    }
Share Improve this question edited Jul 8, 2015 at 5:04 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jul 8, 2015 at 4:59 Joy AcharyaJoy Acharya 6801 gold badge10 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

By the help of @JDupont And @Kevin Simple, I have solved my problem.
Thanks to these guys.

     public JsonResult GetQuantity(string id)
      {
         // Here id is not used, i will use it to get Quantity and unit for my project purpose.
        string getQuantity = "";  
        string getUnit = "";       
        string item1="Hello Quantity" 
        string item2="Hello Unit"
        getQuantity = item1.ToString(CultureInfo.InvariantCulture);
        getUnit = item2.ToString(CultureInfo.InvariantCulture);
        return Json(new { getQuantity, getUnit },JsonRequestBehavior.AllowGet);
   }


    // View

  function GetQuantity(id) {
        var targetDiv = '#divYarnDistributionViewer';
        var url = "/STSM/STSA/YarnDistribution/GetQuantity/" + id;
        var form = $("#frmYarnDistributionViewer");
        var serializedForm = form.serialize();
        $.post(url, serializedForm, function (result) {
            $("#YarnDistribution_StockQuantity").val(result.getQuantity);
            $("#YarnDistribution_StockUnit").val(result.getUnit );

        }, "json");

      return false;
    }

Yes,just like this in your controller:

return Json(resulst = new {Quantity = 5, Price = 285,...}, JsonRequestBehavior.AllowGet);

In your Js,

var Quantity = result.Quantity; var Price = result.Price;

You can return an object with multiple properties as JSON. Lets say you have a basic object with some properties:

public class Stock{
    public string Quantity { get; set; }

    public string Quantity1 { get; set; }
}

You can fetch some stock from wherever your data source is and map the quantity to your Stock object. For simplicity we will just create a hardcoded Stock object:

public JsonResult GetAllQuantities()
{
   Stock stock = new Stock() {
       Quantity = "2",
       Quantity1 = "3"
   };
   return Json(stock, JsonRequestBehavior.AllowGet);
}

To see how the Json is being sent to the client you can always print it to the console in the callback of your ajax request. Be sure to change your request from post to get as you are simply fetching data.

发布评论

评论列表(0)

  1. 暂无评论