Obtain View name in Controller, ASP.NET MVC

前端 未结 4 865
悲&欢浪女
悲&欢浪女 2021-01-25 05:07

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.

The

4条回答
  •  鱼传尺愫
    2021-01-25 05:28

    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 { //....... ; }
    }
    

    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"]);
    }
    

    And in your View a normal ActionLink like so

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

提交回复
热议问题