Calling RenderPartial from an Area - c#

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?

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.

Access view information from master page

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;

C# .NET include file

I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.

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.

ASP.NET/MVC: Inline code

What am I doing wrong? How come <%= this %> isn't being interpreted as C#?
Here's the code :
And here is what it renders (notice the Firebug display):
What do you think is going on? MVC newb here. :(
And the static Site class:
(If you cannot see the screenshots on the page, view source and use the URLs from the <img> tags.)
<%: %> starts with .NET v4
For pre-v4 it's equivalent is <%= Html.Encode(...) %>
The problem was that I was using <%= %> (or even <%: %>) within a tag that had runat="sever".
Shouldn't that be <% %> or <%= %> for a shorthand of Response.Write?
Here's an MSDN article on Embedded Code Blocks.
This sometimes happens to me when embedding code inside of html attributes. I've never quite pinned down the exact cause but sometimes you can get around it by using single quotes rather than double.

Categories

Resources