What is difference between partial and render partial view ? - asp.net-mvc

I am very new in asp.net MVC kindly let me know where is should use partial view and where i should Render Partial view . Thanks in advance

This link might help.
Html.RenderPartial
This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.
This method returns void.
Simple to use and no need to create any action.
RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.
For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.
#{Html.RenderPartial("_Comments");}
This method is faster than Partial method since its result is directly written to the response stream which makes it fast.
Html.Partial
Renders the partial view as an HTML-encoded string.
This method result can be stored in a variable, since it returns string type value.
Simple to use and no need to create any action.
Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model.
For example: In a blog to show comments of an article, you can use Partial method since an article information with comments are already populated in the view model.
#Html.Partial("_Comments")

Both of these helper method are used for rendering partial views
Both have different syntax in razor view #Html.Partial("_student", items) and {Html.RenderPartial("_student", items);}, since render partial return void and the output is written directly to output stream, it has different syntax than Partial.
Syntax in web from view
<%:Html.Partial("_student") %> and <% Html.RenderPartial("_student"); %>
Partial Returns MVCHtmlString, which can be assigned to variables.
Performance wise Render partial is better as it writes directly to output stream.
And of-course you can find a lot of references online and within stack overflow to read

Related

Why do we use #html.action(string,object)?

I really don't understand the usage of Html.Action(string,object) its return type is HTMl string so why do we even need it? and whats its relation with partialviewresult ? I have seen some people using #html.action(String actionname,Object routeobject); in any of their view and controller invoked by this method returns a partialviewresult whats that ?
Firstly, HtmlString represents an HTML-encoded string that should not be encoded again. HtmlString Class
There is a answer that I believe is good from this question:
#Html.Action and #Html.RenderAction are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.
More, use Html.Action when you actually need to retrieve additional data from the server to populate the partial view
Please have a look at the documentation for more detailed information.
More questions about the same topic:
How can I use Html.Action?
MVC Html.Partial or Html.Action
Maybe a bit of research before posting a question would be better

When I call html.renderpartial do I need to specify my model on the first line?

I would like to use renderpartial but I'm not so clear on the way it is used. If I just call render partial then is any model sent to that? I want to use the HTML helpers. Is there any difference between using those in renderpartial and in the main razor view?
If the view actually uses a model and you don't provide any, it just end up having a null Model object (so lots of Null Object Reference Exception).
Just go with
#{ Html.RenderPartial("_partialView", PartialViewModel); }
or:
#Html.Partial("_partialView", PartialViewModel)
The differences between the two are indicated here.
HTH, M.

The model item passed into the dictionary is of type

I have an error that I kind of understand, but can't figure out to solve in the right way.
I have a MasterPage, from that MasterPage I call:
<% Html.RenderPartial("Tags"); %>
Tags is a strongly typed view that looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<JegManglerEn.Controllers.CategoryCount>>" %>
I return the PartialView like this:
return View("Tags", result);
where result is correct and of type:
List<CategoryCount>
The error is this:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[JegManglerEn.Item]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[JegManglerEn.Controllers.CategoryCount]'.
So the setup is this:
MasterPage loads a View that takes a collection of JegManglerEn.Item AND also tries to load a PartialView that takes a collection of a JegManglerEn.Controllers.CategoryCount...but fails to do so.
I know it has something to do with the RenderPartial method because if I change it to RenderAction it's works great.
If you guys have the answer I get to skip looking at the MVC soruce or startup Reflector.
Thanks!
When you call the single-parameter overload of Html.RenderPartial, you're implicitly passing the entire model of the parent view down to the partial. This doesn't work, as the model types expected by the views do not match.
The simplest way to get RenderPartial working in your context is to create a composite model type that contains both the Tags and the page model data, then call the overload where you pass a part of the parent model to the partial:
Html.RenderPartial("Tags", Model.Tags);
That being said, I think RenderAction is most likely the way to go here, as it makes it easier to avoid duplicate code in your controllers (of course assuming you're loading the tags data in the same way for all pages inheriting from the masterpage in question).
Change the
ViewUserControl<IEnumerable<JegManglerEn.Controllers.CategoryCount>>
to
ViewUserControl<List<JegManglerEn.Controllers.CategoryCount>>

Encapsulating User Controls in ASP.NET MVC

Sorry if this is a basic question - I'm having some trouble making the mental transition to ASP.NET MVC from the page framework.
In the page framework, I often use ASCX files to create small, encapsulated chunks of functionality which get inclded in various places throughout a site. If I'm building a page and I need one of these controls - I just add a reference and everything just works.
As far as I can tell, in MVC, the ASCX file is just a partial view. Does this mean that wherever I want to add one of these units of functionality I also have to add some code to the controller's action method to make sure the relevant ViewData is available to the ASCX?
If this is the case, it seems like a bit of a step backwards to me. It means, for example, that I couldn't just 'drop' a control into a master page without having to add code to every controller whose views use that master page!
I suspect I'm missing something - any help would be appreciated.
Thanks,
- Chris
As far as I can tell, in MVC, the ASCX
file is just a partial view. Does this
mean that wherever I want to add one
of these units of functionality I also
have to add some code to the
controller's action method to make
sure the relevant ViewData is
available to the ASCX?
Yes.
However, you can use a RenderAction method in your view instead of RenderPartial, and all of your functionality (including the data being passed to the sub-view) will be encapsulated.
In other words, this will create a little package that incorporates a controller method, view data, and a partial view, which can be called with one line of code from within your main view.
Your question has been answered already, but just for sake of completeness, there's another option you might find attractive sometimes.
Have you seen how "controls" are masked on ASP.NET MVC? They are methods of the "HtmlHelper". If you want a textbox bound to "FirstName", for example, you can do:
<%= Html.Textbox("FirstName") %>
And you have things like that for many standard controls.
What you can do is create your own methods like that. To create your own method, you have to create an extension method on the HtmlHelper class, like this:
public static class HtmlHelperExtensions
{
public static string Bold(this HtmlHelper html, string text)
{
return "<b>" + text + "</b>\n";
}
}
Then in your view, after opening the namespace containing this class definition, you can use it like this:
<%= Html.Bold("This text will be in bold-face!") %>
Well, this is not particularly useful. But you can do very interesting things. One I use quite often is a method that takes an enumeration and created a Drop Down List with the values from this enumeration (ex: enum Gender { Male, Female }, and in the view something like Gender: <%= Html.EnumDropDown(Model.Gender) %>).
Good luck!
You can render a partial view and pass a model object to.
<% Html.RenderPartial("MyPartial", ViewData["SomeObject"]);
In your partial view (.ascx) file, you can then use the "Model" object (assuming you've inherited the proper object in your # Control deceleration) to do whatever you need to with that object.
You can, ofcourse, not pass and Model and just take the partial view's text and place it where you want it.
In your main view (.aspx file), you will need to define the proper object in the ViewData that you're passing to the partial view.
Another method you can do is use:
<% Html.RenderAction("MyAction", "MyController", new { Parameter1="Value1"}) %>
What the previous method does is call a controller Action, take its response, and place it where you called the "RenderAction()" method. Its the equivalent of running a request against a controller action and reading the response, except you place the response in another file.
Google "renderaction and renderpartial" for some more information.

RenderAction RenderPartial

From what I have understood there is a big difference between the Html.RenderPartial included in the ASP.NET MVC release and the HTML.RenderAction in the Microsoft.Web.Mvc.ViewExtensions included in MVC Futures.
On my application I have many pages composed from many "widgets" (sort of) each having its own specific function.
It seemed to me more reasonable to use the RenderAction method as each widget would have a dedicated controller responsible for getting different data and rendering a dedicated view (as opposed to having only one controller and a unique view model to pass to RenderPartial helper to render views).
From the tests I have done having a form that points to a Create action method in a controller like:
<% using (Html.BeginForm("Create", "Message", FormMethod.Post,
new { id = "messageCreateForm" })) {%>
and calling it with
<% Html.RenderPartial("MessageForm",new MessageDTO()); %>
will render correcly a:
<form id="messageCreateForm" method="post" action="/Message/Create">
but with the same equivalent with RenderAction (so using a MessageForm action method on the controller to render the view) would not render correcly so:
<% Html.RenderAction<MessageController>(m => m.MessageForm()); %>
will render in:
<form id="messageCreateForm" method="post" action="">
Note that the action is empty.
Is this the correct way to use the RenderAction helper and is it correct to use it in cases like that?
UPDATE:
Actually renaming the partial view to _MessageForm renders the form correcly.
Very old one, but it jumped into my list of unanswered questions :)
There is a big difference between RenderAction and RenderPartial. RenderPartial will render a View on the same controller (or a shared one), while RenderAction will actually perform an entire cycle of MVC, that is: it will instantiate the controller (any controller you mention, not just the current one), it will execute the action, and it will then return and render the result.
The RenderPartial is more similar to an inclusion, it will even share the same model if you don't specify a different one.
The RenderAction is much more complex (and there may be undesired side effects, that's why they did not make this function available since version 1 -- initially it was available as an experimental feature).
So in your case, if you have widgets, it's OK to use both. It depends on the complexity of the widget. If you have one that has to get data from a DB, do something complex, etc... then you should probably use RenderAction.
I have a News controller responsible for news objects. I created a Block action, which will render a block with the latest news to be put in the home page. This is a perfect example, in my opinion, for RenderAction.
Working with MVC requires much attention to not to shoot yourself in the foot. I mean it by the efficiency of the MVC products. In complex projects I would prefer to use RenderPartial rather than RenderAction. I use RenderPartial in which I use jQuery.ajax request (with Html.Action). It definitely works more efficiently than RenderAction. By this way you can put your Views into cache and then call jQuery.ajax. Try it yourselves.
Ayende explains it clearly in Hibernating Rhinos.

Resources