Access view information from master page - c#

I have a master page and would like to print out the view it is currently being used to render. For instance, someone types in /path/method into the browser. In my master page, I'd like to to print <%= "path" %> somewhere and <%= "method" %> somewhere.
How can I get access to this information from the master page?

At the controller of the view you are accessing:
ViewBag.ViewName = "MyViewName" or ViewData["ViewName"] = "MyViewName"
Then use it at the MasterPage
<%= ViewBag.ViewName %> or <%= ViewData["ViewName"] %>

This is more like what I was looking for:
IView view = ((ViewPage)this.Page).ViewContext.View;
string viewname = ((WebFormView)view).ViewPath;

Related

How to view HTML file in Partial View of ASP.NET MVC (not Razor)

I have an application in which I have created a partial view as below:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
and my parent view has the following code:
<div>
<% #Html.RenderPartial("ViewerControl"); %>
</div>
Now, I want to open an HTML file in the partial view. I am not sure how to do it. Quick sample code will be highly appreciated.
Views do not support server side include directives or similar. Your best bet would be to create an action result that returns the markup as a ContentResult.
public ContentResult HtmlFile() {
return Content(File.ReadAllText(Server.MapPath("Give the path here")));
}
Then in your view:
<%: Html.Raw(Html.Action("HtmlFile")) %>
Totally off the cuff, but you get the point: invoke a server side action to retrieve your markup, or alternatively deliver it via the Model on the previous Action Result execution.

How to check the existance of a value from asp designer page?

I'm getting a list of string from db like:
AddCustomer,
AddUser,
ListCustomer,
ListUser
These are the prefix of asp pages.
I need to hide and show certain pages in the page. Following is the html snippet:
<li>Customer Management
<ul>
<%if (AddCustomer) //how to check whether my string is present or not, is it possible?
{ %><li>Add Customer</li><%} %>
<li>List Customer</li>
</ul>
</li>
Have you tried writing method then calling it from aspx page?
<%# YourMethodName((string)Eval("AddCustomer")) %>

C# - If statement to dynamically determine which master page to use - MVC 2

I have two master pages in my C# MVC application. What I would like to be able to do is use one, or the other depending on the users 'role'. Something similar to this (obviously with a little more validation etc):
<% if(User.IsInRole("One")) { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/One.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } else if { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Other.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } %>
I've seen answers where this can be done to elements of a page, for example a menu, an image, etc. Is it possible to do it for the entire master page? In my situation, depending on the role, different css, images, colours will be used so it is necessary to use a different master page.
If anyone could help I'd be very grateful, or if anyone has any alternative (and probably better) solutions I'd also be grateful.
Thanks.
As you are using ASPX View in ASP.net MVC Application.
ASP.net MVC ASPX ( Webform) view still derive from Page class so you can use following code in
your aspx view.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script language="C#" runat="server">
protected void Page_PreInit(object sender, EventArgs e)
{
if (User.IsInRole("Admin"))
{
this.MasterPageFile = "~/Views/Shared/Site2.Master";
}
else
{
this.MasterPageFile = "~/Views/Shared/Site.Master";
}
}
</script>
You can change it dynamically via ViewMasterPage.MasterPageFile.
I would suggest making the selection in your Masterpage file rather than selecting which masterpage file to use.

Calling RenderPartial from an Area

This is my folder structure
I wan to call a partialview from my view on my area
You could specify the full location of the view when rendering it:
<% Html.RenderPartial("~/Areas/User/Views/SomeController/Foo.ascx"); %>
UPDATE:
And in order to call a partial from the Shared folder:
<% Html.RenderPartial("~/Views/Shared/Foo.ascx"); %>
geocine, please try this one.
<% Html.RenderPartial(Url.Content("~/Views/Shared/Foo.ascx")); %>
Does it solve your problem?

HTML markup rendering issue in ViewData? Also, MultiView functionality in MVC?

i'm trying to populate a ViewData instance with html markup like shown below. When the page renders, the html tags get rendered as text and not html markup. Anyone know why?
Controller code:
if (user.ActivationStatus == false)
{
...
ViewData["Message"] = "<p>Your account has been activated, you're good to go.</p>";
}
else
{
ViewData["Message"] = "<p>Sorry, this account has already been activated.</p>";
}
return View();
View code:
<h2>Confirmation</h2>
<p><%: ViewData["Message"] %></p>
Secondly, I have used the MultiView feature in asp.net webforms in the past. This functionality is ideal and like to implement a similar functionality in MVC.
Is there any way i can call different PartialViews dependant on the function outcome(like the 'if' statement above) from controller code to a placeholder in the View?
<%: %> == Html.Encode(ViewData["Message"]);is HTML Encoding your string...
Try this:
<%= ViewData["Message"]%>
Also, you should be able to control Partial Controls using ViewData and depending on the values in ViewData you could render different partials. You could also strongly type your view to a model that represents the behavior your after on the page...
You can create an MvcHtmlString
<%: MvcHtmlString.Create(ViewData["Message"]) %>
But really you should not have the html in your controller.
By making your view strongly-typed you could instead do something like this.
<h2>Confirmation</h2>
<% if(Model.ActivationStatus) { %>
<p>Sorry, this account has already been activated.</p>
<% } else { %>
<p>Your account has been activated, you're good to go.</p>
<% } %>
Change your view output to:
<h2>Confirmation</h2>
<p><%= ViewData["Message"] %></p>
Only use <%: %> syntax when you want output to be automatically HTML encoded.

Categories

Resources