Skip to content
Loading
  • Prakash Kumar
    +1
    Suman,
     
              Remove HttpGet Method, than return View with Model like this,
    1. public ActionResult GetProductID(int Eid)  
    2. {  
    3.     var prodId = _productModuleAPI.GetProductId(Eid);  
    4.     LandingViewModel lvm2 = new LandingViewModel();  
    5.    _productModuleAPI = new ProductModuleAPI();  
    6.    var productModules = _productModuleAPI.GetAll(Eid);  
    7.    lvm2.ProductsModulesViewModel = productModules.ToList();  
    8.    return View(lvm2);  
    9. }  
    In View Page use Model like,
     
    1. @model IEnumerable
     then use,
       
    1. @if (Model.productsViewModel != null)  
    2. {  
    3.     foreach (var item in Model.productsViewModel)   
    4.     {  
    5.         class="collapse navbar-collapse" id="bs-sidebar-navbar-collapse-1">  
    6.         class="nav navbar-default">  
    7.               class="">"@item.ID" onclick="productClick(this)"                      @*href="@Url.Action("BLogic", "Login",new {  [email protected]})"*@>@item.PRODCTNAME"font-size:16px;"  class="pull-right hidden-xs showopacity ChangeButtonColorOrangeRed  glyphicon glyphicon-off">  
    8.              
    9.   
    10.           
    11.     
      
  •    }  
  • }  
  • else  
  • {  
  •      
    no products
      
  • }  
  •  
    +1
  • Prakash Kumar
    0
    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. 
     
    0
  • 0
     Thank you salman and prakash for responding,
     i have written script like this
    0
  • 0
    am getting values i.e (list) but it is not hitting view page when am debugging
    0
  • Prakash Kumar
    0
    Hi Suman can you show your controller and view?
    0
  • Salman Beg
    0
    @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. 
    0
  • 0
    @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);
    }
    0
  • Ravi Patel
    0
    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 
    0
  • Manas Mohapatra
    0
    Where is the problem now.. Is that AJAX request hitting to controller action and returns result..
    0
  • Prakash Kumar
    0
    Hi Suman,
     
             Call your function productClick() in page load method, then only the  controller method will  call,
          
    First thing, your using ajax method and also using Model in view use any one mehtod. 
     
    1. $( document ).ready(function() {  
    2.     productClick();  
    3. });  
     
    0
  • 0
    Yes Manas, it is not hitting my view page
    0
  • Harshal Patil
    0
    Using Ajax call u can get it
     var Selected ='test';
    $.ajax({
    type: 'POST',
    url: '@Url.Action("GetProdList")',
    dataType: 'json',
    data: { statecode: Selected },

    success: function (Ros) {
    $("#dropdownID").append('');
    $.each(Ros, function (i, Ro) {
    $("#dropdownID").append('');
    });
    },
    error: function (ex) {
    alert('Failed to retrieve Ro code.' + ex);
    }
    });
     
    and on .cs site
     
    public JsonResult GetProdList(string statecode)
    {
          var prodcode=GetallProdcode();
         List Pcodlst = new List();
          foreach (var prod  in prodcode)
          {
             Pcodlst .Add(new SelectListItem { Text =prod.name, Value = prod.id });
          }; 
    return Json(Pcodlst , JsonRequestBehavior.AllowGet); 
    }
    0
  • Bikesh Srivastava
    0
    Hi,
    Everyone is given good example.it will resolve your query,
    but you need to use html to contains json  data e.g textarea... 
    0
  • 0
    function productClick(el) {
    var Eid = $(el).attr("id");
    $.ajax({
    cache: false,
    type: "GET",
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    url: "/Login/GetProductID",
    data: { Eid: Eid },
    success: function (data) {
    debugger;
    alert(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert("Ajax Failed!!!");
    }
    }); // end ajax call
    }
    0
  • Vineet Kumar
    0
    you can use ajax for the same. I am showing you how you can append result to html div tag using jquery :
     
    $(document).ready(function(){ 
    $.ajax({
    url: "home/getproductIid?eid=306",
    type: "GET", 
    contentType: "application/json; charset=utf-8",
    success: function (data) {
    $("#mydata").Html(data);  //where "mydata" is div id
     
    }
    });
      });
     
     Or you can also parse json using $.parseJSON() method to fetch properties
    0