我怎样才能从调用视图控制器方法 [英] How can i call a controller method from view
问题描述
我有一些类似的要求,我需要创建一个jQuery grid.I创建的jqGrid的,对于我的jqGrid需要从控制方法方法可能返回int或字符串(而不是另一种观点)得到一些动态值。当我使用@ Url.Action(法,控制器)调用控制器方法。我没有得到值和code部分没有执行。
I have some requirement like, i need to create a jquery grid.I created the JQgrid, for that JQgrid i need to get some dynamic values from the controller method That method may return int or string (not an another view).When i am calling the controller method using @Url.Action("method", "controller").I am not getting value and that code part is not executing.
function setrownumJQ() {
var rownum = 0;
rownum = '@Url.Action("method", "controller")';
return rownum;
}
Method in controller:
int setRowNum()
{
var r = 4;
return r;
}
注意:此方法将返回一个int值
Note: This method will return a int value.
推荐答案
您刚才讲的URL,但你不叫吧..解决方案是使用jQuery的阿贾克斯$,并调用动作方法
You just told the URL, but you not called it.. solution is use $.ajax of jquery and call the action methods
function setrownumJQ() {
var rownum = 0;
var url = '@Url.Action("method", "controller")';
$.ajax({
url: url
}).done(function (data) {
rownum = data
});
return rownum;
}
这篇关于我怎样才能从调用视图控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!