I cannot add a partial view in vs2010 - visual-studio-2010

When I try to add a partial view using asp.net mvc 3 and razor it just add's a normal .cshtml file and not a partial view. has anyone had this problem before?
/Martin

In Razor there is no notion of partial views as there was in WebForms. You only have templates with the .cshtml extension. Depending on how the controller action serves this view (with either return View() or return PartialView()) the _Layout.cshtml will be applied or not. Also if you render a template with either the Html.Partial helper this template will be considered as a partial view. Finally you have the possibility to set whether you want or not a Layout from inside the template itself. For example to disable it:
#{
Layout = null;
}
So basically in Razor you have templates and you could consider partial views as templates without a layout.

Related

localization for mvc ajax form and partial view

I have used resources file to create multi langual mvc3 application. In _viewStart.cshtml I have these two lines which makes it that loclization works for all views except partial views which is rendered from a ajax.form
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(AsoMvcApp.MySession.Current);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(AsoMvcApp.MySession.Current);
when I check the CurrentUICulture for a partial view which is rendered from an action which is called from a ajax form it is still default and not chnaged. when I add those to lines of code in the begining of the partial views it works fine. but I don't want to add it to all partial views. it must be a better way.
it must be a better way.
Yes, instead of putting this code in your _ViewStart.cshtml put it in the Application_AcquireRequestState method in your Global.asax

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")

ASP.NET MVC 3 - pass data to a partial view in Layout

I'm working on a ASP.NET MVC 3 application, but I'm rather new to MVC in general.
I have a partial view in a my application layout view that needs to have data passed to it. this will appear on every page. Is there a way to make this happen so I don't have to load that data into the view model for every action in the entire site?
As in, if a user navigates to Mysite/admin/settings, I would like to have the partial view on the layout be able to somehow receive the data that it needs without me needing to put that code in the Settings action in the Admin controller.
On this same note, how do you pass data to the layout view of an application anyway?
In these situations I usually use a base ViewModel for my Views
public class ApplicationViewModel
{
public string UserName {get; set;}
....
}
public class SettingsViewModel : ApplicationViewModel
{
}
all your views would inherit from that ViewModel. Your layout would expect it as well
_layout.cshtml:
#model ApplicationViewModel
....
<h1>hello #Model.UserName</h1>
hopefully this answers your question
Partial only renders a view. You need to provide the model manually.
You can create an action for the view you want and render it with Html.Action( actionName ).
Make an action for example menu which will create a model that will be provided to the menu view.
Now you can call the #Html.Action("menu") from wherever, and it will be rendered autonomously. (you can ofcourse provide a controller name as well, and even custom routeData)
You might also want to set Layout = null; in the view to avoid using the master layout of the whole site.
This is how I pass a value to the partial view from my layout page:
Layout page code:
Html.RenderPartial("_SubMenuLeft", new ViewDataDictionary { {"category", "MMG"} });
and in my _SubMenuLeft.cshtml (partial view)
#if (ViewData["category"] == "MMG")
{
...
}
Hope it helps someone for future reference.

ASP.NET MVC 3 Partial View dynamically rendered and linked from dynamic list in view

In my MVC 3 application, I will have a view that will contain a partial view. The view itself will have a list of dynamically generated links. The link has to cause the partial view to render detailed information for that linked item.
Would I use Ajax for this? If so, since I haven't worked with Ajax before, is there any documentation for using it in a MVC 3 app?
Also when the view is first loaded, the partial view will either not be loaded or ideally show another separate partial view. Any thoughts on a good way of doing this?
Thanks for the help.
Create an action method which returns a PartialViewResult:
[HttpGet]
public ActionResult DetailedLinkInfo(int someIdentifier)
{
var detailedLinkInfo = GetFromSomewhere();
return PartialView(detailedLinkInfo );
}
Then create a partial view, strongly-typed to the type of detailedLinkInfo (let's say it's an DynamicLink.
#model WebApplication.Models.DynamicLink
#* bunch of HTML for the detailed info *#
Then use jQuery on the client-side. Give all your links a class so it makes it easier to hook up the event:
$(function() {
$('a.dynamic-link').click(function() {
$.get('/SomeController/DetailedLinkInfo', someIdentifier: $(this).attr('id'), function(data) {
$('#some-div').html(data);
});
});
});
End result: you click one of the links, the jQuery will perform an AJAX GET to your controller action, then bind the result to the div.
The easiest way of solving this problem that I found was using Ajax helpers that come with the MVC 3 framework. The Ajax video for MVC 3 on Pluralsight did a phenomenal job at succinctly explaining the basics of how to use this feature.

How does ASP.NET MVC arbitrate between two identically named views (aspx and razor)?

Using ASP.NET MVC3 I created a new Razor view and gave it the same name as the existing .aspx view that I had been using. I noticed that controller continued to pick up the .aspx view (which has the same name as the action) which is pretty much what I expected. I then renamed the .aspx view and action picked up the razor .cshtml view.
So if I have two views called myview.aspx and myview.cshtml and an Action called MyView() that does a return View(), it will pick up the myview.aspx view and return that.
How does MVC3 decided which view-type to default to?
Is there a way to change this default behavior to prefer a razor view over an .aspx view?
Everything stems down to the order of view engines in the ViewEngines.Engines collection. Here's how the ViewEngines static constructor looks like (as seen with Reflector in ASP.NET MVC 3 RTM):
static ViewEngines()
{
ViewEngineCollection engines = new ViewEngineCollection();
engines.Add(new WebFormViewEngine());
engines.Add(new RazorViewEngine());
_engines = engines;
}
which explains why WebForms is the preferred view engine.
So you could perform the following grotesque hack in Application_Start to inverse the preference towards Razor :-)
var aspxVe = ViewEngines.Engines[0];
var razorVe = ViewEngines.Engines[1];
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(razorVe);
ViewEngines.Engines.Add(aspxVe);
I would imagine its down to the order in which view engines are registered. Earlier registered view engines will be queried first. If you want to change the order:
ViewEngines.Engines.Insert(0, ...);

Resources