Skip to content
Loading
How to return Json result to razor view in mvc 4 
  • Manas Mohapatra
    Where is the problem now.. Is that AJAX request hitting to controller action and returns result..
  • Ravi Patel
    Hi,
     
    refer this  
     
    https://www.asp.net/mvc/videos/mvc-2/how-do-i/how-do-i-return-json-formatted-data-for-an-ajax-call-in-an-aspnet-mvc-web-application 
  • @prakash and @salman
     
    my view (above script included in this)
     
    @if (Model.productsViewModel != null)
    {
    foreach (var item in Model.productsViewModel)
    {
    }
    }
    else
    {
    no products
    }
     
     
    controller
     
    ActionResult BLogic()
    {
    LandingViewModel lvm2 = new LandingViewModel();
    _productAPI = new ProductsAPI();
    var products = _productAPI.GetAll();
    lvm2.productsViewModel = products.ToList();
    }
    [HttpGet]
    public JsonResult GetProductID(int Eid)
    {
    var prodId = _productModuleAPI.GetProductId(Eid);
    LandingViewModel lvm2 = new LandingViewModel();
    _productModuleAPI = new ProductModuleAPI();
    var productModules = _productModuleAPI.GetAll(Eid);
    lvm2.ProductsModulesViewModel = productModules.ToList();
    return Json(lvm2, JsonRequestBehavior.AllowGet);
    }
  • Salman Beg
    @suman
    Please write success and error function inside your ajax call so that you can easily get the data.
    $.ajax({
    url: '/Login/GetProductID',
    type: "GET",
    dataType: "json",
    data: { Eid: Eid },
    success:function(result)
    {
    console.log(result);
    },
    error:function()
    {
    console.log(error);
    }
    });
     
    thanks. 
  • Prakash Kumar
    Hi Suman can you show your controller and view?
  • am getting values i.e (list) but it is not hitting view page when am debugging
  • Prakash Kumar
    1. function productClick(el) {  
    2.        var Eid = $(el).attr("id");  
    3.        $.ajax({  
    4.            url: '/Login/GetProductID',  
    5.            type: "GET",  
    6.            dataType: "json",  
    7.            data: { Eid: Eid },  
    8.            success:function(result)  
    9.            {  
    10.                  console.log(result);  
    11.            }  
    12.        });  
    13. }  
    check like this
  •  Thank you salman and prakash for responding,
     i have written script like this
  • Prakash Kumar
    Hi Suman,
     
    Method 1:
          Call Controller action by ajax method
     
    Method 2: 
          Return View with model by controller   
    1. [HttpGet]  
    2. public JsonResult GetProductID(int Eid)  
    3. {  
    4.     _productModuleAPI = new ProductModuleAPI();  
    5.     var productModules = _productModuleAPI.GetAll();  
    6.     lvm2.ProductsModulesViewModel = productModules.ToList();  
    7.     return View(lvm2);  
    8. }  
    and use script in view to get values.   
    1.   
     
    Hope it will work. 
     
  • Salman Beg
    Hi.
    if you are calling this method from a ajax call then automatically the result will come in success when you send json data and in the success method you can retrieve all the values.
    thanks.