Click here to Skip to main content
16,004,887 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to call a method from MVC controller in javascript of MVC view Page..

I have controller like this


[HttpGet]
public ActionResult SalesReportDetails()
{
try
{
return View();
}
catch (Exception ex)
{
logger.Error(ex);
TempData["Exception"] = ex;
return RedirectToAction("ErrorPage", "Home");
}
}
public void ShowSimple()
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("~/") + "crystalreport.rpt");
}

and I want to call ShowSimple method in below function

function fnNext() {
var frm = document.forms[0];
frm.selecteditem.value = "Next";
ShowSimple();
return true;
}

is this correct method to call..I am not getting proper solution..

please help...
Posted

You can the MVC action from Javascript by sending an jQuery ajax request.
Change ShowSimple as ActionResult
C#
public ActionResult ShowSimple()
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("~/") + "crystalreport.rpt");
var data=new {Status="Success"};
retunt JSon(data);
}


JavaScript
function Javascriptfunction()
  {
     $.getJSON('/Controller/ShowSimple/', function (data) {
alert(data);
});
  }

C#
function fnNext() {
var frm = document.forms[0];
frm.selecteditem.value = "Next";
Javascriptfunction();
return true;
}


Hope this helps
 
Share this answer
 
v3
Comments
Apali 20-Jul-13 0:48am    
thanks a lot and when I am using this code I am not getting solution means that javascript function is not called...can you pls tell??
Jameel VM 21-Jul-13 12:30pm    
what is your pblm now?
Apali 23-Jul-13 0:40am    
means control is not coming to java script function after using url: '/Controller/ShowSimple/', code..how to do..
Jameel VM 23-Jul-13 1:15am    
which control?
Apali 23-Jul-13 1:43am    
means that showsimple method is not called after using url: '/Controller/ShowSimple/' this in my javascript code
C#
public ActionResult ShowSimple()
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("~/") + "crystalreport.rpt");
var data=new {Status="Success"};
retunt JSon(data,JsonRequestBehavior.AllowGet);
}


JavaScript
function fnNext() {
var frm = document.forms[0];
frm.selecteditem.value = "Next";
$.ajax({
      url: '@Url.Action("ShowSimple")',
      type: 'GET',
      cache: false,
      data: { Data U want to Send },
      success: function (data) { 

                     },
      error: function (data) {   
                     }
      });
return true;
}

Make an Ajax Call
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900