Html.RenderPartial call from masterpage - asp.net

Here is a scenario: Let's say I have site with two controllers responsible for displaying different type of content - Pages and Articles. I need to embed Partial View into my masterpage that will list pages and articles filtered with some criteria, and be displayed on each page. I cannot set Model on my masterpage (am I right?). How do I solve this task using Html.RenderPartial?
[EDIT]
Yes, I'd probably create separate partial views for listing articles and pages, but still, there is a barrier that I cannot and shouldn't set model on masterpage. I need somehow to say "here are the pages" as an argument to my renderpartial, and also for articles. Entire concept of renderpartial with data from database in masterpages is a bit blurry to me.

How about creating an HtmlHelper extension method that allows you to call a partial view result on the an action on the controller.
Something like
public static void RenderPartialAction<TController>(this HtmlHelper helper, Func<TController, PartialViewResult> actionToRender)
where TController : Controller, new()
{
var arg = new TController {ControllerContext = helper.ViewContext.Controller.ControllerContext};
actionToRender(arg).ExecuteResult(arg.ControllerContext);
}
you could then use this in your master page like
<% Html.RenderPartialAction((HomeController x) => x.RenderPartial()) %>
and in your controller the appropriate method
public PartialViewResult RenderPartial()
{
return PartialView("~/Path/or/View",_homeService.GetModel())
}
Well that is my 2 cents anyway

I had a similar post and came up with an object model to handle it.
I HATE the non-strongly typed views so went with this approach and it is working well.

I think that your solution may lie in the land of MVC 2.0 RC and beyond...
Phil Haack posted an article on his blog: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

The ViewData Model property should only be used for the content that you're viewing/editing on the main section of the UI.
Other parts of the view may need some data present in ViewData, so just add those to the dictionary.
I'd just pass data from the dictionary like this: ViewData["articles"] to the partial. (or ViewData.Get() from MvcContrib).
You might also look at the recently implemented SubController pattern implemented in MvcContrib.

yes, this is correct. but let's look at this scenario:
on views that are related to articles, I'd have ViewData["article"], and on views related to pages, I have ViewData["pages"], but I don't have both articles and pages available all time. So, if I add:
Html.RenderPartial("articlesView", ViewData["articles"])
Html.RenderPartial("pagesView", ViewData["pages"])
to my masterpage, I'll have an exception thrown on each page on which ViewDataDictionary doesn't contain both articles and pages.
At least, that's how I see it.

The way that I handle this is by using a BaseViewModel. All Views are strongly typed against a view model that inherits from BaseViewModel.
The BaseViewModel class has all of the information needed by the MasterPage. So for navigation your BaseViewModel may look like this:
public class BaseViewModel
{
public BaseViewModel()
{
NavigationItems = RetrieveNavigationItemsFromModel();
}
public List<NavItems> NavigationItems {get; set;}
}
In your MasterPage and PartialViews, you can cast the Model to BaseViewModel and access the NavigationsItems property.
<ul>
<% foreach (NavItem ni in (Model as BaseViewModel).NavigationItems) { %>
<li>
<%= ni.DisplayText %>
</li>
<% } %>
</ul>

This is a very late reply, but I got to this page whilst googling - so chances are someone else will see this question (and my reply) as well.
The way I've worked around this problem is by using a simple jQuery script to load a PartialView and execute it's controller code. Sample below.
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(document).ready(function() {
$("#applicationForm").load("/Home/ApplicationForm");
});
</script>
<div id="applicationForm" />
</asp:Content>
The big drawback to this approach is that the client has to have scripting enabled for it to work (so it's really SEO unfriendly). If that's something you can live with it works well. I only use it on an intranet site where I know that each client has JavaScript enabled and I don't have to worry about google's bots.

Related

MVC with Article Web Site

I am creating a Web site that will have a lot of articles. I created a ArticleController for the Web site. As an example, the first article to be implemented is Machine Language. So I created a view called machine_language.cshtml.
public class ArticleController : Controller
{
// GET: Article
public ActionResult Index()
{
return View();
}
public ActionResult Machine_Language()
{
return View();
}
}
So far in the controller I have to code above.
So my question is, I have 500 articles that need to be converted to start with. Normally, I created an ASPX page for each article, but now I am creating a view, since I am using MVC.
So does that mean I will need to have 500+ ActionResults in the ArticleController? One for each view/article.
Before I get started I want to make sure I'm doing this correctly, because it seems rather strange. If I have to create an action result with each article's name. I will end up having a huge ArticleController. Once there are a couple thousand articles, things will get seriously out of control.
Edit: P.S. I like to use pages for the articles. Since they are static content. I am trying to stay away from having huge articles with images, videos, etc... in a database.
How are you retrieving "article data/text"? In this particular situation, I'd have a View Model that has ArticleText as a property, and then you might need other article-specific items (tags? Author(s)? etc.).
Then pass this View Model from your controller's action to the view. The view would then be in charge of rendering this particular article to the user.
With this particular design, you'd have a single action in a single controller passing data (a single view model) to a single view.
In my opinion, that is the most scalable and the best approach.
Step away from the web forms thought where a single "viewing experience" equals on a "single entity" in the backend. That's the beauty and power (and scalability) of MVC.
I would recommend you that to render your existing aspx page to the view that you have using iframe. Since you have already static pages with you. And you can simply pass the page name as parameter for each article and you can achieve what you want. It's an alternative approach. Even if you want you can add images to those aspx pages and display the same here

ASP.NET MVC - Code Behind of Master Pages

I am newbie for ASP.NET MVC 1.0. I am converting from a classic application built up with VS2008 .NET3.5. I created a master page, and the menu must be read from the database. Now the code that generate the HTML into the appropriate menu div in classic ASP.NET3.5 VS2008 was in the code behind of the master page.
I cannot understand now where the code beind of the master page is in ASP.NET MVC 1.0?
Anyone has examples?
Thanks
In MVC there are no longer Code-Behind classes. What you want is a Partial.
You'd use it like so:
<% Html.RenderPartial("MainMenu.ascx", ViewData["Menu"]); %>
If this Menu is going to be in all of your pages you can make your controllers subclass a custom controller class that always fills the Menu data first.
If messing with the MVC inheritance hierarchy is overkill you can also make a MenuController class and use the RenderAction in your view/master:
<% Html.RenderAction<MenuController>(x => x.MainMenu()); %>
You can still have code behind if you want. In your .master file put:
<%# Master Language="C#" AutoEventWireup="true"
Inherits="Site_Master" CodeFile="Site.Master.cs" %>
Then in your .master.cs:
public partial class Site_Master : ViewMasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Your master page is now a View, and Views should be passive. In other words, they shouldn't go look up things themselves.
It would be a much more correct approach (within the context of ASP.NET MVC) to pull the required data from the Model.
Take a look at this SO question for a related discussion.
There is a great tutorial on the ASP.NET site that shows how to do exactly this.
Briefly, you pass the data to the master page through the ViewData collection. To get the data into ViewData, create an application level controller. Have the page controllers inherit from the application controller instead of the base MVC controller.
Also, if you need to do things on your master page in reaction to the page being displayed, through this application controller you can tie into the ActionExecuting event. That will provide you information about the context of the page request currently being process.
Personally, I prefer using strongly typed views and ViewModels. If your master page requires data, then create a ViewModel for it. Ensure that every page's ViewModel inherits from this base ViewModel. Similarly, create a base controller that every other controller inherits from. Using Action Filters will allow you to ensure that the master ViewModel is populated implicitly. See this for an example.

what is the equivalent of the "literal" tag in asp.net mvc

i am migrating a website from asp.net to asp.net mvc
in the asp.net site there are many places where they have "literal" tags and the server generates a bunch of html and sticks in in the literal tag.
what is the equivalent of doign this in asp.net mvc. Should i shove this in ViewData?
The equivalent is to not make the same bad design choices the original developer made.
But, since we live in the real world, you'd shove their strings-which-contain-html into the presentation model for a particular page and then write it to the response stream.
In your Model:
public class MyPageModel
{
public string HolyCrapItsHtml {get;set;}
}
In your controller:
public ActionResult MyPage()
{
return View(new MyPageModel
{HolyCrapItsHtml = OldCode.GetHtmlICantBelieveIt()});
}
And in your page:
<div>
In the olden days, we'd concatenate our webpages together from strings like:
<%= Model.HolyCrapItsHtml %>
</div>
I had the same problem. Don't bother with a MyPageModel, just stick with ViewData but use equals instead of the colon.
<%= ViewData("SomeVarName") %>
If you're using Razor, you'd do the following if not using strongly-typed helpers
#Model.YourVarName
If you're working in with strongly-typed view model, you can use the ValueFor() helper
#Html.ValueFor(m => m.YourVarName)
If you just have a few literals, you could put them in the ViewData, but if you're following a true MVC model you should create a model for the view with corresponding properties. Considering you're porting from an existing ASP.NET page, setting up a new model for each new view/page may carry a lot ov overhead, in which case stick it in the ViewData.

ASP.NET MVC ViewResult vs PartialViewResult

What is the difference between the controller result named ViewResult and PartialViewResult? More importantly, when is the PartialViewResult used?
PartialViewResult is used to render a partialview (fx. just a user control). This is pretty nifty for AJAX stuff, i.e.
<script type="text/javascript">
$.get(
"/MyController/MyAction",
null,
function (data) { $("#target").html(data) }
);
</script>
and action
public ActionResult MyAction()
{
return PartialView("SomeView");
}
where SomeView is a MVC User Control, e.g.:
<div>
<%= DateTime.Now.ToString() %>
</div>
http://msmvps.com/blogs/luisabreu/archive/2008/09/16/the-mvc-platform-action-result-views.aspx
In practice, you’ll use the
PartialViewResult for outputing a
small part of a view. That’s why you
don’t have the master page options
when dealing with them. On the other
hand, you’ll use the ViewResult for
getting a “complete” view. As you
might expect, the Controller class
exposes several methods that will let
you reduce the ammount of typing
needed for instanting these types of
action results.
Generally speaking, ViewResult is for rendering a page with optional master, and PartialViewResult is used for user controls (likely responding to an AJAX request).
none of the existing answers actually answer the question "What is the difference".
The differences are as follows:
1) the locations where the view engine will attempt to find the view:
for ViewResult, it's in ViewLocationFormats and MasterLocationFormats
for PartialViewResult, it's in PartialViewLocationFormats
2) ViewResult has the additional property MasterName
that is all.
There are several cases where you would want to break down your view into several small components. One use case that I am working with right now, is I have a multi-lingual site that I would like to reload content using AJAX principles.
Normally what I would do in the case of a non-multi lingual site is to create another ActionResult to return the ViewModel that is changing with the new parameters. I like to use a custom ActionResult that I have called JsonpResult. The problem resides in the fact that I have labels not in my database but in Resource files. So what I would need to do is to somehow hydrate my Resource file data into the ViewModel.
Once the data comes down the pipe, my AJAX callback handles the wiring up of the ViewModel response back to the HTML page using Javascript (I use jQuery).
This definitely works, however it becomes a question of maintainability. I now need to not only maintain my original ASP.NET view, but I also need to maintain a set of scripts that handle AJAXian behavior. If you need to have your site SEO, then you really need to make sure that both the Server Side and Client Side behavior are both working the same.
This is where Partial Views come into play for me. What I do is "pull out" the logical data sections where the bulk of the reload occurs. The nice thing about PartialView is that you can pass your ViewData and Model along to the PartialView. If your PartialView is strongly typed against your ViewModel you can get Intellisense to help with the wiring of the PartialView.
Now all I need to do with my AJAX call is to write the response back to a single DIV rather than handling data points individually. What it does mean is that there would be more content coming down the pipe. However, the trade off is easier to read and maintain code.
The one of the main differences is PartialViewResult doesn't use _ViewStart.cshtml. The code from _ViewStart.cshtml file executes at the start of rendering before any code in the view.

How do you use usercontrols in asp.net mvc that display an "island" of data?

I am trying to find out how to use usercontrols in asp.net mvc. I know how to add a usercontrol to a view and how to pass data to it. What I haven't been able to figure out is how do you do this without having to retrieve and pass the data in every single controller?
For example, if I have a user control that displays the most recent posts on several but not all the pages in the site, how do I write the Controllers so that I get data for that usercontrol and pass it to the user control from only one place in the web site instead of getting and passing data in each of the different controllers that the user control is used in?
I'm not sure if this makes sense or not. Is there a better or recommended way to handle an "island" of data that you want to display on several pages?
I'm coming from web forms where I could just write a user control that got its own data and displayed data independently from the rest of whatever page it is used on.
There are multiple ways to do it.
The basic approach is
Populate the data for the view in the BaseController (OnActionExecuting event)
Writing a custom action filter
Writing an Application Controller (the eg. is in the below links).
An example of OnActionExecuting will be
[HandleError]
public class BaseController : Controller
{
CourseService cs = new CourseService();
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
List<Tag> tags = cs.GetTags();
ViewData["Tags"] = tags;
}
}
You can use the "tags" view data on any view. This is just an example of usercontrol being rendered as side content.
<div id="sidebar_b">
<asp:ContentPlaceHolder ID="ContentReferenceB" runat="server" >
<% Html.RenderPartial("Tags"); %>
</asp:ContentPlaceHolder>
</div>
I found the following URL to be useful.
http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx
http://blog.matthidinger.com/2008/02/21/ASPNETMVCUserControlsStartToFinish.aspx
http://www.aaronlerch.com/blog/2008/01/26/displaying-foo-on-every-page-of-an-aspnet-mvc-application/
http://blog.wekeroad.com/2008/01/07/aspnet-mvc-using-usercontrols-usefully/
In the MVC Futures, available on codeplex , contains the RenderAction HtmlHelper extensions. This will allow you to create a controller for the ueser control and this controller will populate the ViewData used by the user control without having to resort to a base controller as was suggested.
In the View you would do
<% Html.RenderAction("Index", "UserControlController") %>
or one of the other overloads.
This will create an instance of the controller, execute the method and render the user control view into the main view. The main view controller does not need to know anything about the user control or its model/data.
Refactor the code that obtains the view data for this user control into it's own method, maybe even it's own model (class). Call this method from each controller that needs to populate the control and pass the results in the ViewData with a well-known key. You might even want to pass the type of the current controller to your method in case it needs to know what data to retrieve based on the base model for the controller.
ViewData["RecentPosts"] = RecentPosts.GetRecentPosts( this.GetType() );
In your control, retrieve the data using the well-known key.
How to Handle "Side Content" in ASP.NET MVC

Resources