我已经写了下面在我的局部视图jQuery的:
$阿贾克斯({ 键入:POST, 网址:'@ Url.Action(PostActionName,ControllerName)', 数据:{ID:01}, 成功:功能(数据) { 如果(data.success =真) { window.location的='@ Url.Action(GetActionName,ControllerName)' } } });动作的名称和控制器名称并不固定,就必然取决于其中,该局部视图放在视图改变。我有函数获取调用动作和控制器的名字,但不知道如何可以通过它们@ Url.Action。
以下是Javascript函数来获取行动和控制器的名称:
函数ControllerName(){ VAR pathComponents = window.location.pathname.split('/'); VAR controllerName; 如果(pathComponents.length&GT = 2){ 如果(pathComponents [0]!=''){ controllerName = pathComponents [0]; } 其他{ controllerName = pathComponents [1]; } } 返回controllerName; } 功能ActionName(){ VAR pathComponents = window.location.pathname.split('/'); VAR actionName; 如果(pathComponents.length&GT = 2){ 如果(pathComponents [0]!=''){ actionName = pathComponents [1]; } 其他{ actionName = pathComponents [2]; } } 返回actionName; }解决方案
我具备的功能来获取调用动作和控制器的名称,但 不知道我怎样才能把它们放入@ Url.Action
那么,你可以调用这些函数。例如,如果它们是扩展方法的UrlHelper类:
window.location的='@ Url.Action(Url.MyFunction1(),Url.MyFunction2())',或者如果他们只是静态的功能:
window.location的='@ Url.Action(SomeClass.MyFunction1(),SomeClass.MyFunction2())'如果另一方面需要被传递仅可以执行下列操作在客户端上已知的值:
VAR参数='在客户端上已知的一些动态值';VAR URL ='@ Url.Action(SomeAction,SomeController,新{someParam =__param__});window.location.href = url.replace('__ param__',连接codeURIComponent(参数));更新:
看来你只是想获取电流控制器和行动可能这样实现:
@ { 字符串currentAction = Html.ViewContext.RouteData.GetRequiredString(行动); 字符串currentController = Html.ViewContext.RouteData.GetRequiredString(控制器);}和则:
window.location.href ='@ Url.Action(currentAction,currentController)';I have written following jquery in my partial view:
$.ajax({ type: "POST", url: '@Url.Action("PostActionName", "ControllerName")', data: { Id: "01" }, success: function(data) { if (data.success="true") { window.location = '@Url.Action("GetActionName", "ControllerName")' } } });The Action name and Controller name are not fixed, they are bound to change depending upon the view wherein this partial view is placed. I have functions to fetch invoking action and controller names, but not sure how I can pass them in @Url.Action.
Following are Javascript functions to fetch action and controller names:
function ControllerName() { var pathComponents = window.location.pathname.split('/'); var controllerName; if (pathComponents.length >= 2) { if (pathComponents[0] != '') { controllerName = pathComponents[0]; } else { controllerName = pathComponents[1]; } } return controllerName; } function ActionName() { var pathComponents = window.location.pathname.split('/'); var actionName; if (pathComponents.length >= 2) { if (pathComponents[0] != '') { actionName = pathComponents[1]; } else { actionName = pathComponents[2]; } } return actionName; }解决方案
I have functions to fetch invoking action and controller names, but not sure how I can pass them in @Url.Action
Well, you could call those functions. For example if they are extension methods to the UrlHelper class:
window.location = '@Url.Action(Url.MyFunction1(), Url.MyFunction2())'or if they are just static functions:
window.location = '@Url.Action(SomeClass.MyFunction1(), SomeClass.MyFunction2())'If on the other hand the values that need to be passed are known only on the client you could do the following:
var param = 'some dynamic value known on the client'; var url = '@Url.Action("SomeAction", "SomeController", new { someParam = "__param__" })'; window.location.href = url.replace('__param__', encodeURIComponent(param));
UPDATE:
It seems that you are just trying to fetch the current controller and action which could be achieved like that:
@{ string currentAction = Html.ViewContext.RouteData.GetRequiredString("action"); string currentController = Html.ViewContext.RouteData.GetRequiredString("controller"); }and then:
window.location.href = '@Url.Action(currentAction, currentController)';
更多推荐
如何传递@ Url.Action动态值?
发布评论