在控制器,ASP.NET MVC获取视图名称 [英] Obtain View name in Controller, ASP.NET MVC

查看:188
本文介绍了在控制器,ASP.NET MVC获取视图名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net的MVC。我有4页,显示事件列表(不同类型的事件),并在每一页上详细信息链接指向EventDescription.aspx视图。

I am using asp.net mvc. I have 4 pages that show list of events(different type of events) and "Details" link on each page leads to "EventDescription.aspx" View.

在EventDescription.aspx有一个叫做回到事件有采取用户到相应的页面链接。

The "EventDescription.aspx" has a link called "Back to Events" which has to take the user to the appropriate page.

例如:如果详细信息的要求,从第1页的回到事件来链接需要指向Page1.aspx的。同为page2.aspx,page3.aspx,page4.aspx。

Eg: if the "Details" request came from page1 the "Back to Events" link needs to point to page1.aspx. Same for page2.aspx,page3.aspx,page4.aspx.

我的计划是显示EventDescription.aspx和用户的回到事件链接的ViewData值之前捕捉控制器动作EventDescription视图名称(page1.aspx这个),并存储在ViewData的。

My plan is to capture the view name (page1.aspx) in controller action "EventDescription" and store in ViewData before displaying the "EventDescription.aspx" and user the ViewData value for "Back to Events" link.

如何从那里请求来自内部行动取得视图的名称?

How to obtain the name of the View from where the request comes from inside a Action?

感谢在前进。

推荐答案

我建议你使用TempData的,而不是ViewData的。例如,你可以有这样的设置。

I suggest you use TempData instead of ViewData. For instance you could have a setting like this.

public ActionResult Details(int id)
{
   var event = repository.GetByID(id);
   if (event != null)
   {
      TempData["ReturnPath"] = Request.UrlReferrer.ToString();
      return View(event);
   }
   else { //....... ; }
}

而在你查看你可以有一个常规的ActionLink像这样

And in your View you could have a regular ActionLink like so

<% =Html.ActionLink("Back To Events", TempData["ReturnPath"]) %>

如果你想成为干,你也可以在你的控制器创建操作方法只处理重定向像这样。

If you want to be DRY, you could also create an Action method in your controller just to handle the redirects like so.

public ActionResult GoBack()
{
    return Redirect(TempData["ReturnPath"]);
}

而在你查看一个正常的ActionLink像这样

And in your View a normal ActionLink like so

<% =Html.ActionLink("Back To Events", "GoBack") %>

这篇关于在控制器,ASP.NET MVC获取视图名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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