I've noticed there are several ways to use Views and PartialViews:
RenderAction, RenderPartial, and "return PartialView"
RenderAction when placed inside HTML, will simply call an Action and Render the View returned (the View returned can be partial view or view?)
RenderPartial will simply retrieve the contents of a View without executing any Controller action
Finally, what's the difference between "Return View" and "Return PartialView"?
Thanks
return View() returns the view with a Layout enabled so you get full HTML page with <html> and <body> tags. return PartialView() on the other hand disables the Layout and you get only the HTML fragment contained in this view. Actually when working with the Razor view engine I prefer to talk about templates and not views and partial views. That's because a view is a template and a partial view is a template without a layout. But in both cases it's a Razor template.
Related
I'm building a new website and one of the requirements is to have the login form be a modal window. I'm trying to include it in the top navigation bar and it's only being rendered if the user is not logged in.
How can I add this modal window with it's own model inside the top navbar? Are there any alternatives?
If I delete the model and let an empty modal everything works perfectly but when I add it again it doesn't work, because the model of the page (in this case the index page) is a different one then the one from the modal login.
P.S. I'm using Razor Pages and ASP.NET Core 2.2.
Partial View
So you make a _LoginPartial.cshtml file. and let's say you set #model LoginViewModel
Inside this _LoginPartial.cshtml you have your Login Modal and all the functionality.
Now when you invoke your partial inside an Index page that has model #model AnotherModel, you need to pass a new model to the partial like so:
<partial name="_LoginPartial" model='new LoginViewModel()' />
name is the name of your cshtml page.
model is the #model of the page.
Read More Here
View Components
To be brief if you take this route this is essentially like nesting a little controller inside your page. Allowing you to change scope for your #model.
Read more about View Components
Update for nested objects
You need to instantiate the object property.
<partial name="_LoginPartial"
model='new LoginViewModel { InputModel = new InputModel() }' />
I have a Single Page Application (SPA) developed using ASP .net MVC and Durandal.
I have a view Test.html and corresponding view Test.js.
My requirement is I want to display this view within another view (using as a template) and also display this view for a route - say http://localhost/MyApp/#Test.
Any suggestions on how can I achieve this?
My problem is, in the view I need to return the instance of my view model if I want to display the view for the route. And if I do this, I cannot use this as a template within another view.
You can handle this by using the Durandal compose knockout binding.
<div data-bind="compose: { view: 'views/myview.html', model: object }"></div>
Here you can bind anything you want to object as your view.
See #3 here for more information: http://durandaljs.com/documentation/Using-Composition.html#composing-explicit-models-and-views
Can an ASP.NET MVC PartialView directly use a Layout?
MSDN
I have looked at MSDN for PartialViewResult and compared with ViewResult:
ViewResult has a property MasterName that PartialViewResult does not.
But.. I can still define the Layout property in the partial views Razor.
Background
I am currently remediating a large code base with a huge amount of partial views used to fill iframes. Ideally they would be converted to normal views (ideally we would not use iframes) but I was wondering about just adding a layout to these partials to take out the <head> element at least so that we have more control over the script versioning (everything is replicated in each partial view). I'm looking for a light-touch solution since much of the code is expected to be thrown away.
Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout.
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Yes.
You can use a layout within a layout:
Layout
View
Layout
PartialView
We have an example of this in our codebase.
I want to render a PartialView inside a <iframe src=''></iframe> The rendered partial view has its own JavaScript code and the CSS sheet. I tried two ways to get this work (none of them worked):
1)
<iframe src="http://localhost:54351/Box/19"></iframe>
public PartialViewResult Box(int id)
{
return PartialView(GetBox(id));
}
Result: the plain text (string), there is no the CSS sheet and the JavaScript code doesn't work
====================================
2)
<iframe src="#{ Html.RenderPartial("~/Views/Box.cshtml", #Model); }"></iframe>
Result: Obviously it's doesnt work,
nothing shows inside the iframe
In the first solution, I was wondering if is it possible to return maybe a RazorView object (or something) which will have working JavaScript code and the CSS sheet. Any ideas ?
I believe you need to return a full View rather than a PartialView. Partial views don't automatically pick up the layout (because they are designed to slot into an existing page). Since an IFrame is completely independant of the parent page, it will need it's own stylesheet and script reference tags.
public ActionResult Box(int id)
{
return View(GetBox(id));
}
I think you are misunderstanding both partial views and iframes.
An iframe renders an entire web page, not just a partial one, inside of another web page. The iframe must have a complete URL that is a different page from the current page (if it was the same page, it would try to render the iframe within itself over and over creating an infinite loop).
What you need to do is specify the URL of a different action that returns a full view. If you render a partial view, there is no <head> tag, and therefore no script tags typically associated in a head tag. no <link> tags, no title, etc...
I have a Microsoft MVC project with an action "Foo" whose view ("Foo.aspx") contains the lines:
<%= Html.ActionLink("mylinktext1", "bar") %>
<%= Html.ActionLink<MyController>(x => x.Bar(), "mylinktext2") %>
When I hit this from a web browser or load it from an AJAX call, it properly returns:
mylinktext1
mylinktext2
But when I call the action from another view like this:
<% Html.RenderAction<MyController>(x => x.Foo()); %>
Then the links are rendered without targets.
mylinktext1
mylinktext2
Why would this be happening, and how do I work around it?
I don't know if that is what you are doing wrong, but I have never used Html.RenderAction with actions that return ASPX views. When I call RenderAction, I make sure that I am calling a controller action that returns ASCX View User Control.
Typically .ASPX file is an entire page and you can't (shouldn't) render this inside another page.
So I think you should make it View User Control (ASCX) and put it either in Shared or in controller's view folder.
Based on your comment:
Of course this is fine. You just return your data as model to your views/view user controls. When you load them thru AJAX, you should consider implementing Render View to String. Search the Google or Stack for more information on it. You can also search for a thing called JsonPox attribute for your action methods - also implemented somewhere on the internet. It will enable decorating your action methods so that they are able to return HTML view, XML or JSON if that's what you also might need.