如何在MVC视图页面的javascript中调用MVC控制器的方法.. [英] How to call a method from MVC controller in javascript of MVC view Page..

查看:63
本文介绍了如何在MVC视图页面的javascript中调用MVC控制器的方法..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVC视图页面的javascript中调用MVC控制器的方法..



我有这样的控制器





[HttpGet]

public ActionResult SalesReportDetails()

{

try

{

返回查看();

}

catch(例外情况)

{

logger.Error(ex);

TempData [Exception] = ex;

返回RedirectToAction(ErrorPage,Home );

}

}

public void ShowSimple()

{

ReportDocument reportdocument = new ReportDocument();

reportdocument.Load(Server.MapPath(〜/)+crystalreport.rpt);

}



我想在下面的函数中调用ShowSimple方法



函数fnNext(){

var frm = document.forms [0];

frm.selecteditem.value =下一步;

ShowSimple();

返回true;

}



这是正确的方法来调用..我没有得到正确的解决方案..



请帮忙......

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...

推荐答案

你可以通过发送一个来自Javascript的MVC动作jQuery ajax请求。

将ShowSimple更改为ActionResult

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







function Javascriptfunction()
  {


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




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





希望这有帮助



Hope this helps


public ActionResult ShowSimple()
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("~/") + "crystalreport.rpt");
var data=new {Status="Success"};
retunt JSon(data,JsonRequestBehavior.AllowGet);
}







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


这篇关于如何在MVC视图页面的javascript中调用MVC控制器的方法..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆