如何从部分视图操作获取主控制器和操作名

时间:2021-01-17 21:09:45

I have a controller:

我有一个控制器:

public class LanguageController : Controller
{
 [HttpGet]
 public ActionResult Index()
 {
   // populate viewModel from database
   return PartialView(viewModel)
 }

 [HttpPost]
 public ActionResult Index(string language)
 {
    LanguageCookie.Write(Response, language);
    return RedirectToAction(ACTION, CONTROLLER, new {culture = language});
 }
}

and its partial view:

和它的局部视图:

@model MyModel
@using (Html.BeginForm("Index", "Language"))
{
  @Html.DropDownList(
    Model.SelectedLanguageShortName, 
    Model.AllLanguages
  )
  <input type="submit" value="Select" />
}

which I render in _Layout.cshtml:

我在_Layout.cshtml中渲染:

<div>
  @Html.Action("Index", "Language")
</div>

Please let me know how can I get ACTION/CONTROLLER names of main (not partial) controller, from my LanguageController was called. I need this information on the postback where I set cookie and want to redirect user on the same page but with prefered language.

请让我知道如何从调用我的语言控制符获取main(而不是分部)控制器的操作/控制器名称。我需要回发上的这些信息,我在回发中设置了cookie,并希望在相同的页面上使用首选语言重定向用户。

I have found this example:

我发现了这个例子:

var rd = ControllerContext.ParentActionViewContext.RouteData;
var currentAction = rd.GetRequiredString("action");
var currentController = rd.GetRequiredString("controller");

But ControllerContext.ParentActionViewContext is null in the postback. I am able to get what I need in the view but it is ugly:

但ControllerContext。在回发中,ParentActionViewContext为空。我可以在视图中找到我需要的东西,但它很丑:

@Html.Hidden("Controller", HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString());
@Html.Hidden("Action", HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString());

How to get the same information in the controller scope?

如何在控制器范围内获取相同的信息?

1 个解决方案

#1


1  

When Index(string language) is processed ParentActionViewContext is null because this is another request to server and it doesn't know anything about previous request that invoked child action.

处理完ParentActionViewContext时,索引(字符串语言)为null,因为这是对服务器的另一个请求,它不知道调用子动作的任何先前请求。

Instead of storing control and action in hidden field you can store the whole address and invoke Redirect:

与在隐藏字段中存储控件和操作不同,您可以存储整个地址并调用重定向:

@model MyModel
@using (Html.BeginForm("Index", "Language", new { redirectUrl = Request.Url }))
{
    @Html.DropDownList(
        Model.SelectedLanguageShortName, 
        Model.AllLanguages
    )
    <input type="submit" value="Select" />
}

and then

然后

[HttpPost]
public ActionResult Index(string language, string redirectTo)
{
   LanguageCookie.Write(Response, language);
   return Redirect(redirectTo);
}

Another way is to save CONTROLER and ACTION in TempData, but in this way you can have problem if somebody open multiple pages of your site.

另一种方法是在TempData中保存控制和操作,但是如果某人打开了您的站点的多个页面,就会出现问题。

Third solution is to invoke that method with Ajax and when response arrive reload the page with javascript.

第三种解决方案是使用Ajax调用该方法,当响应到达时,使用javascript重新加载页面。

#1


1  

When Index(string language) is processed ParentActionViewContext is null because this is another request to server and it doesn't know anything about previous request that invoked child action.

处理完ParentActionViewContext时,索引(字符串语言)为null,因为这是对服务器的另一个请求,它不知道调用子动作的任何先前请求。

Instead of storing control and action in hidden field you can store the whole address and invoke Redirect:

与在隐藏字段中存储控件和操作不同,您可以存储整个地址并调用重定向:

@model MyModel
@using (Html.BeginForm("Index", "Language", new { redirectUrl = Request.Url }))
{
    @Html.DropDownList(
        Model.SelectedLanguageShortName, 
        Model.AllLanguages
    )
    <input type="submit" value="Select" />
}

and then

然后

[HttpPost]
public ActionResult Index(string language, string redirectTo)
{
   LanguageCookie.Write(Response, language);
   return Redirect(redirectTo);
}

Another way is to save CONTROLER and ACTION in TempData, but in this way you can have problem if somebody open multiple pages of your site.

另一种方法是在TempData中保存控制和操作,但是如果某人打开了您的站点的多个页面,就会出现问题。

Third solution is to invoke that method with Ajax and when response arrive reload the page with javascript.

第三种解决方案是使用Ajax调用该方法,当响应到达时,使用javascript重新加载页面。