mvc3 - using partial views in a different area - asp.net-mvc-3

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.

Related

nopCommerce Index Page -> How does it render all the sections?

I am looking at the nopCommerce source code. Somehow the front office page shows Categories, Manufacturers, Poll, Featured Product, Search etc... however I am just looking at the source code of the Index.cshtml and I can't see any code to do this. The source code of the Index is attached. The link to the demo page is http://demo.nopcommerce.com/
I was expecting the Index page to render partial views or will have the code to create these sections but nothing there.
Can anyone explain how nopCommerce renders the views?
Is there any docs to explain technical side of nopCommerce. I have read the user guide but it does not have any details.
#{
Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}
<div class="page home-page">
<div class="page-body">
#Html.Widget("home_page_top")
#Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
#Html.Action("HomepageCategories", "Catalog")
#Html.Action("HomepageProducts", "Catalog")
#Html.Action("HomepageBestSellers", "Catalog")
#Html.Action("HomePageNews", "News")
#Html.Action("HomePagePolls", "Poll")
#Html.Widget("home_page_bottom")
</div>
</div>
This is nothing unique to nopCommerce, Html.Partial & Html.Action are normally used in all ASP.NET-MVC applications. Partial and Action act similarly to render reusable partial views. Only difference is that Partial works with current model, while Action retrieves additional data.
HomepageCategories, HomePagePolls etc views are located under Views > Catalog > HomepageCategories.cshtml inside NopCommerce application folder and if you'd like to change them, you can copy the View folder into your theme folder and edit it there so you don't have to modify original files.

Adding feedback feature to all pages

I have got asp.net mvc 3 website, I want to add feedback feature to all pages.
I created the partial view for this purpose and render it in master layout.
#model FeedbackHelper
Name:<br />
#Html.TextBoxFor(o=>Model.Name)
for example in Questions page , MVC returns the exception because that page binded the POST entity, as far as I check in StackOverflow I have got 2 solution
create a parent model and add POST and FeedbackHelper as properties
use Tuple
at the moment, changing all models is too risky for me.
Is there any good solution ?!
You could use child actions. The idea is to define a specific controller action that will serve the partial view and then include it using the #Html.Action helper in your Layout.
So:
public ActionResult Feedback()
{
FeedbackHelper model = ...
return PartialView(model);
}
then you will of course have a partial in the Shared folder:
#model FeedbackHelper
Name:<br />
#Html.TextBoxFor(o => o.Name)
and include it in your Layout:
#Html.Action("Feedback", "ControllerContainingTheFeedbackAction")

How to get view model directly in layout?

I'm working on a practice where I minify most of my javascript in static files, and then each view has something like this:
#section Script
{
#Html.Action("MinifyJavaScript", "Resource", new { viewPath = "~/Views/User/Register.Js.cshtml", model = Model })
}
Which in turn renders the tiny, non-static, pieces of javascript code, like the one below:
#model UserRegisterModel
<script>
(function ($, b) {
$(function () {
b.views.user.register('#Url.Action("ValidateInput", "User")');
});
})(jQuery, bruttijjimo);
</script>
This allows me to cache javascript in views more heavily, since only the parts that vary with the model can change (and are generally treated as partial views)
Now I'd like to further upgrade this practice by removing the need of a layout section, and by convention, render the javascript partial view (which is passed the same model as the view) right after the view. I already created the method to compress the javascript in the view. And there's also the convention that javascript for a view must be in a .js.cshtml file, and share the model with the view.
What I'd need is to grab the model for the view from the layout, and the name of the view, too, and render it there.
This would only work or be needed for the actual view, since partials use a script manager if they need to emit javascript.
So: how can I grab the model for the view from the layout? The rest I can figure out, but this one is eluding me..
So: how can I grab the model for the view from the layout?
#Model should give you what you are looking for.
This being said I didn't quite understand what you are doing but the fact that you have javascript inside your view instead of having it in a separate javascript file and leaving the caching stuff to the browser and reinventing some wheels kind of feels wrong.

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.

.Net MVC3 Getting a Reference to the Page from a Controller

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.

Resources