How can i get view path in ASP.NET MVC - c#

I create a helper and i want to get view's path in this helper code, How can i do that?
I try code below. It will use view that return from action. So If i use this helper in partial view it will get parent view name instead.
RazorView view = helper.ViewContext.View as RazorView;
viewPath = view.ViewPath;
Thank you in advance.

You can retrieve the path of the view or partial view using
string path = (helper.ViewDataContainer as WebPageBase).VirtualPath;
This will return something like ~/Views/Home/MyPartial.cshtml

Related

partial view not displaying but its viewmodel works on parent

In the cshtml code header I've instantiated a viewmodel with some data. I can bind to and display data from that viewmodel variable in the parent page. However, when I pass the viewmodel variable into a child partial view, nothing displays on the page.
#{
Html.Partial("DailyReport", #DailyReportViewModel);
}
Any idea why the child partial view would not be displaying on the page or how to debug this issue?
Because you are calling the Partial method which return an MvcHtmlString, but you are not using the return value of the method.
You can do this instead
#Html.Partial("DailyReport", #DailyReportViewModel);
Razor will now render the return value from the DailyReport method.
Or you can use the RenderPartial method which render the result.
#{
Html.RenderPartial("DailyReport", DailyReportViewModel);
}
Assuming DailyReportViewModel is an object of type T to which your partial view is strongly typed to.
Put # at the front and remove the # from DailyReportViewModel and it should work.
#Html.Partial("DailyReport", DailyReportViewModel);

Sending Temporary data to multiple partial views from view MVC

I'm wondering if there is a good way to do this. I'm currently trying to send some temporary data to multiple partial views being called from the same view page in my MVC application.
I'm currently attempting to do this with TempData but I can see my understanding is limited as it is only going through for one partial request. What method do I need to use to filter out to all of my partials?
Main View Page:
#{
ViewBag.Title = "Main View Page";
TempData["ReturnUrl"] = Request.Url.OriginalString.ToString();
}
#Html.Partial("_StatusTable1")
#Html.Partial("_StatusTable2")
#Html.Partial("_StatusTable3")
#Html.Partial("_StatusTable4")
#Html.Partial("_StatusTable5")
Partial View Example:
#{
var temp = TempData["ReturnUrl"]; // temp is null on all partials except the first
}
// Partial View Code ...
Thanks in advance.
In your main view page call the partiel views like that
#Html.Partial("_SomePartial", TempData["ReturnUrl"])
I think that even this would work.
#Html.Partial("_SomePartial", TempData)
Get the value from TempData like this. ReturnUrl Value will retained across all the Partial Views
#{
var temp = TempData.Peek("ReturnUrl");
}
// Partial View Code

Returning Partial View ASP.NET MVC

Hi I have a partial view setup for handling error messages, but when it's returned it returns a full webpage with just the text No Results Found.
I would expect the Partial View to return inline on my main Index page? As in my Index page I have the code #Html.Partial("_ErrorMessage").
Below is how I'm handling this in my HomeController
else
{
ViewBag.Error = "No results found!";
return PartialView("ErrorMessage");
}
}
Partial view not necessary return html inline. Partial view can return html inline if you call by client script, using jquery, angular, etc.
This way did you make. The return is a whole page.
Context is important here. If you are including the partial view in line in the HTML, you probably don't need to return the partial view in C-sharp unless you're using a fancy asynchronous call to load it.
The in-line partial view should display normally if you just show the view where it was included
I am not sure what the exactly error you are facing but you can try following solution, you have to set Layout as null after you will not facing the full page on your partial view.
#{
Layout = null;
}
If you are not using Layout=null it will call your _Layout page.
Hope it helps you.

ASP.NET MVC4 #MvcHtmlString.Create() issues with #Html.RenderPartial()

I am trying to dynamically render pages from database. The view looks like this
#model MyModel
#{
ViewBag.Title = Model.Title;
Layout = Layout = "~/Views/Shared/_PageInnerLayout.cshtml";
}
#MvcHtmlString.Create(Model.Content)
MyModel has just 3 properties; Id, Title and Content
If the Content has just HTML, the view renders just fine. But in some cases, I need to render partials too. So Content may contain code like
#{
Html.RenderPartial("_FooterPartial");
}
This does not work. Its being rendered literally, like shown in the image below
How do I fix this so that the page is rendered properly?
The problem is in the difference between Html.RenderPartial and Html.Partial helper methods. RenderPartial directly write the result to HttpContext.Response while Partial return it as a MvcHtmlString. You need to replace the RenderPartial with Partial and maybe the RenderAction with Action.
Edited:
You need to render the content if contains the razor view scripts. If so it means you are actually storing the views or partial views inside the database. You can create a new VirtualPathProvider to help loading the views from the database. For more information see ASP.NET MVC load the Razor views from database and ASP.NET MVC and virtual view
I tried doing this with VirtualPathProvider, but it didn't work.
Finally what I did was to write the Content from database into a temporary view and then render the view. It worked
public ActionResult DynamicPage(int id) {
var dynamicPage = new PagesContext().Pages.FirstOrDefault(s => s.Id == id);
string webroot = Server.MapPath("~").TrimEnd('\\');
string fileName = webroot + "\\Views\\\\Solutions\\Temp.cshtml";
System.IO.File.WriteAllText(fileName, dynamicPage.Content);
return View("Temp");
}
Here, the Content property of the dynamicPage containts HTML(including Razor code)

Getting the Controller and Action used to call

In a Partial View _MyView.cshtml I need to get the Controller name and the Action used to create the parent calling view.
So if /Equity/Sell was the Controller/Action used to call the view which rendered the partial view _MyView.cshtml , then I need the value /Equity/Sell.
I don't see an object that can do that though.
In the view use:
ViewContext.RouteData.GetRequiredString("controller");
ViewContext.RouteData.GetRequiredString("action");
For posterity in a child action it is like this:
ControllerContext.ParentActionViewContext.RouteData.Values["action"];
ControllerContext.ParentActionViewContext.RouteData.Values["controller"];

Categories