.Net MVC3 Getting a Reference to the Page from a Controller - asp.net-mvc-3

What I'd like to do seems pretty simple. I'd like to specify the page title in my #Page directive on a view, like so:
<%# Page Title="About This Site" ... %>
Then, I'd like to get that page title into the ViewData, so that it could be used in the master page, like:
<head>
<title>The Awesome Site | <%=ViewData["Title"]%></title>
And also in the View itself like:
<h1><%=ViewData["Title"]%></h1>
I am already inheriting a custom controller for all of the page controller methods. So I'm hoping that there's something I can do in the controller to transfer the Page.Title property into the ViewData (or maybe I'll use the ViewBag). I just can't seem to find any route to reference the Page from within the Controller.
Is this possible? Is there a different approach I might want to consider.

See "generic errors" post on this for a clean way to do this.
Passing data to Master Page in ASP.NET MVC
you should not be referencing anything from a "page" from a controller. The two are completely separate by design. You need to package your data and then send it to the view.
Another way (more like what you are requesting) is that you use
#{
ViewBag.Title ="my view title";
}
Then the master page uses that value wherever you want it.

I think you might be looking for Html.Title().
You can write this to any part of the page:
<title><%: Html.Title() %></title>
This will pickup the title from your view and insert it into your master page as required.

Related

dynamically load parial list in _Layout razor (master)

I am having trouble with a _Partial in the master _Layout.
My _MemberList (Partial) have its own ViewModel (MemberListViewModel)
and I want to load this Partial in the _Layout maybe through a BaseController with a Layout Attribute or something, which is the best solution for this ?
The _MemberList is dynamically loaded, it depends on what the user selects.
It worked before when I had a strongly typed ViewModel in the _Layout, but when I try to load other ViewModels it asks for the one in the _Layout, so I guess I have to find another solution for this.
So what I need is a "dynamically loaded list in the _Layout(master) using a _partial".
Sample would be great!
Thanx

mvc3 - using partial views in a different area

I have two questions regarding partial views...
When to use Partial views vs #helper methods, i have used both
interchangeably and would like to get more consistent in their
usage. What do you guys do?
How do you reference a partial view from another area.
I have an area called admin and i have a partial view in the regular Views directory. How do i use it .. i have tried the following which dont work as it cant be found.
#Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
Model)
other i have tried -
#Html.Partial("~/Views/ControllerName/_PartialView", Model)
I'm not sure if you mean Html helpers, or razor helpers when you say "helpers" In any case, I only create Html helpers when it's a small, idividual item like a control.
If you mean Razor helpers, then they are different from Partials in that you can call them like functions, passing whatever parameters you want. Partials are largely stuck with the "model" system (and of course Temp/ViewData/Bag.
It's all about how you want to work with the code.
As for your Partial. You have to include the suffix.
#Html.Partial("~/Views/ControllerName/_PartialView.cshtml", Model)
Since the questioner asked about areas here's how to do it in an area
#Html.Partial("~/Areas/Store/Views/Pages/Checkout.cshtml")
I am just giving specific and simple example of what I'm trying to do.
I need to be able to logoff from an area page using the partialview located in the main shared folder. Here's what I did:
At the area view I reference the partial view by
<div class="float-right">
<section id="login">
**#Html.Partial("~/Views/Shared/_LoginPartial.cshtml")**
</section>
</div>
At the Main shared folder where the _LoginPartial code was located I added {new = area ("")}, from:
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", #class = "navbar-right" }))
to:
using (Html.BeginForm("LogOff", "Account", **new { area = "" },** FormMethod.Post, new { id = "logoutForm", #class = "navbar-right" }))
Hope that helps in some way!
Another option is to make the partial view you want to share between areas SHARED.
So you put it in the main ~/Views/Shared/ folder, e.g.
~/Views/Shared/_MyPartialView.cshtml.
You can then refer to it from any area by saying
#Html.Partial("_MyPartialView")
Make sure your Controllers in Areas have the [Area("MyArea")] annotation. As of this post, pulling in Partial Views from across Area boundries via Ajax div updates in ASP.NET Core works for me with Tag Helpers and #Html.ActionLink.

What are Partial Views?

I've been using Codeigniter in order to get accustomed to the Model-View-Controller architecture, and to try and speed up the process of making and implementing sites.
I keep seeing references to "Partial Views" but can't find a definition for the term.
Can anyone tell me what a partial view is, and where it is used?
A partial view is just a sub-view that you can include in a parent view. Let's take a look at a common example:
// Controller:
$data['myvar'] = array('element1', 'element2', 'element3');
$this->load->view('myview', $data);
// Myview:
<ul>
foreach ($myvar as $var) {
$this->load->view('partialview', array('var', $var));
}
</ul>
// Partialview:
<li><?= $var ?></li>
This is useful to repeat content according to a list.
Note that nothing differs between a view and a partialview, it's just the way you include it that defines the term.
The best way to describe a "partial view" is to think of it as a template, it displays a chunk of html with Model data passed to it.
Good examples of where to use one would be where you plan on displaying the same html over and over, like a menu or a page header or even better yet use them to display content requested using ajax.
Basically you call an action on the controller that returns the partial view from lets say jQuery and then put the returned markup into a select or div tag. Here is an example of doing that from my blog easy ajax with aspnet mvc and jquery, yes I know it asp.net mvc not php and codeigniter, but the principal is the same.

Best MVC design for rendering a grails template and passing data to this template?

I have a template that renders a chart according to a given list (retrieved from my domain layer). This template is displayed several times (with a different list) on a same page (let's say "home page") and in several pages as well.
For instance,
<g:render template="/template/chart" model="order: 'asc', orderBy: 'age', max: 5, domainClass: Person"></g:render>
should show a list of 5 persons ordered by age in ascendant order.
But the problem is that I need to construct the list of persons by querying the domain layer and unfortunately this can be done only through the template code like :
<%
def vizuService = grailsApplication.classLoader.loadClass('com.myapp.VizuService').newInstance()
%>
<img src="${vizuService.getChartImage(order, orderBy, max, domainClass)}"/>
//The service method getChartImage will query the DB
This design is flawed because I am retrieving my data through my View layer. But,
I cannot reasonably send the data list to the template through the home page controller because the controller might even NOT know how many charts (and what data) are displayed in the Home page.
So, How can I design it in a MVC manner? (i.e. the Controller should be responsible for getting/passing the data). Is TagLib the only way to do it? (and IMO, TagLib does not really respect MVC principles) Maybe, AJAX is the (only) solution?
Thx for your ideas.
I have solved my problem by using the include tag that allows to include partial views rendered by the response of another controller/action

In a View page within MVC, what is the preferred way to deal with empty variables

When using the MVC design pattern I usually try to make my view files as simple as possible.
Therefore in my View I try not to have lots of code like this:
if page title exists
display page title
else
display 'default page title'
end if
Instead, in my Controller I might use code like this:
if no page title is specified
page title = 'default page title'
end if
load the view (pass page title as a parameter)
Is this the best way to tackle this issue?
Keeping this if-else condition in the controller level is a better idea than moving into views. (if you can otherwise it's alright)
It will make your views look good !!!!

Resources