Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the current view name in ASP.NET MVC?

I have a partial view (control) that's used across several view pages, and I need to pass the name of the current view back to the controller - so if there's e.g. validation errors, I can re-draw the original view.

A workaround way to do it would be (in the controller methods)

var viewName = "Details"; // or whatever ViewData["viewName"] = viewName; return(View(viewName, customer)); 

and then in the partial itself, render it as

<input type="hidden" name="viewName"      value="<%=Html.Encode(ViewData["viewName"])%>" /> 

Question is - is there some property or syntax I can use to retrieve this directly instead of setting it from the controller? I've tried the obvious:

<input type="hidden" name="viewName"      value="<%=Html.Encode(this.Name)%>" /> 

but this doesn't work. What am I missing here?

Thanks.

like image 770
Dylan Beattie Avatar asked Aug 12 '09 21:08

Dylan Beattie


People also ask

How do I return a view?

It doesn't matter if you implicitly return the ViewResult with return View(); or explicitly pass the view name to the View method with return View("<ViewName>"); . In both cases, view discovery searches for a matching view file in this order: Views/\[ControllerName]/\[ViewName]. cshtml.

What does return View () in MVC do?

return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View("~/Views/Account/Register. cshtml") – returns the Register view under /Views/Account folder (view extension name “.

How do I get ViewModel to view?

The correct way to Pass ViewModel to the View The recommended way to pass the ViewModel to the View is to make use of the View method. The View method takes the model as one of the argument, which internally sets it to the ViewData. Model Property.


2 Answers

Well if you don't mind having your code tied to the specific view engine you're using, you can look at the ViewContext.View property and cast it to WebFormView

var viewPath = ((WebFormView)ViewContext.View).ViewPath; 

I believe that will get you the view name at the end.

EDIT: Haacked is absolutely spot-on; to make things a bit neater I've wrapped the logic up in an extension method like so:

public static class IViewExtensions {     public static string GetWebFormViewName(this IView view) {         if (view is WebFormView) {             string viewUrl = ((WebFormView)view).ViewPath;             string viewFileName = viewUrl.Substring(viewUrl.LastIndexOf('/'));             string viewFileNameWithoutExtension = Path.GetFileNameWithoutExtension(viewFileName);             return (viewFileNameWithoutExtension);         } else {             throw (new InvalidOperationException("This view is not a WebFormView"));         }     } } 

which seems to do exactly what I was after.

like image 185
Haacked Avatar answered Sep 17 '22 19:09

Haacked


I had the same problem and that's how I solved it:

namespace System.Web.Mvc {     public static class HtmlHelperExtensions     {         public static string CurrentViewName(this HtmlHelper html)         {             return System.IO.Path.GetFileNameWithoutExtension(                 ((RazorView)html.ViewContext.View).ViewPath             );         }     } } 

Then in the view:

var name = Html.CurrentViewName(); 

or simply

@Html.CurrentViewName() 
like image 27
Konamiman Avatar answered Sep 18 '22 19:09

Konamiman