MVC 2 RenderAction - asp.net-mvc

Im having a value in ViewData, lets say htmlhelper.ViewData["myData"]="some";
And in partial page I can overwrite the myData's value.
But when I using the Html.RenderAction() and call a partial page.
In the same partial page htmlhelper.ViewData["myData"] is null.

When you call RenderAction, you create an entirely new ViewData instance for your partial page. If you want ViewData["myData"] to be visible by your other action, either pass it to the subaction or put it in TempData.

I figured out from MVC source code. Cool that we have MVC as open source.
htmlHelper.ViewContext.HttpContext.Items["myData"]
this will maintain the value from Partial and RenderAction case.

Related

ASP.NET MVC Hybrid of ContentResult and ActionResult

I am after creating something like a ContentResult, but have it render within the #RenderBody() tag of a masterpage. Is this possible? I can't seem to find a clear answer for it.
No; you can't do that.
Instead, you can make an empty Razor view that simply renders HTML from its model.
You can have an action method return a ContentResult, and then invoke it in the master page by doing:
#Html.Action("Action", "Controller")
I'm not sure what you mean by doing it in #RenderBody, but you can do it within the master page itself, or within each content page.
The content will be generated in the page; you could also use a partial view approach too.

ASP.NET MVC - Can a Partial View have a controller?

When I'm in a View and I call #Html.RenderPartial("MyPartialView", MyObject) Can I set it up so that this partial view has a controller which is called when RenderPartial gets called?
Probably it will be better to use the RenderAction instead of the RenderPartial
You should gather all data necessary for the partial in the current controller action (which may use methods shared across other controllers and actions).
If you really want a partial to be rendered using its own controller/action then consider loading it via AJAX with a separate request.
In MVC, although controllers know about views, the reverse is not true.
Views are just means to render some data (a model or a viewModel) but they are not related to a controller or an action.

calling a partial view in asp.net MVC3

What is difference between Html.Partial and Html.action in context of using a partial view in asp.net MVC3 ?
Html.Action will call a controller Action, so it'll go through the whole MVC pipeline (inside the server) again to find a controller that will return a ViewResult (although, you theoretically you can also return a JsonResult or something else) .
Html.Partial will only return a PartialPage (as in a CSHTML file) and won't go through the whole pipeline. It will just search using the view engine.
Some advantages to Action is having Authentication, Caching and other stuff that happens in the MVC pipeline, while Partial is faster (although you might have more responsibility in the partial page if you need to pass a ViewModel etc.)
This is a nice post (a bit old) about pros/cons of RenderAction vs RenderPartial
Html.Partial includes directly the view at the place where you called the helper. It is like a file include.
Html.Action invokes a controller action first which could render a view and it is the result of this action that is included. And because a controller action is invoked a controller needs to be instantiated, so the whole MVC pipeline gets executed as a child request.
You may take a look at the following blog post.
Stackoverflow ninja search got me this:
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
Also see this article
Render Action vs RenderPartial

Obtain the pathName dynamically for ASP.NET MVC

I'm looking for a solution that allows me to get the parent page pathname from a partialView invoked. For example I have an Action baa and a controller boo so the javascript to obtain this will be window.location.pathname, and it returns /boo/baa.
The thing is that I use a partial view, so when I try to retrieve the pathname via the URL helper, what I obtain is the PartialView one, and what I need is the pathname for the action that invokes it. I want to do this because I want to show certain things of the partial view depending on the pathname of the parent view that invokes it. Is there a way to do this with ASP.NET MVC?
Thank you all in advance,
You can access the RouteValues on the ViewContext on the page. This will contain the controller and action values as determined by the routing engine. You might want to consider making the determining information part of your model, however, as this exposes logic in the controller -- what the view does with this information, though, is completely up to it.

ASP.Net MVC Reading Action Parameter/ Route Info in a RenderPartial

In my MVC app I have one action available on a public side so everything looks like http://www.domain.com/pagename
When it goes to the action method of my WebPageController it finds it in the DB by pagename and renders the View.
In the View I do a database call to a foreign key table called WebPageContent. For each WebPageContent I do a RenderPartial(WebPageContentItem.ControlType)
This works for the most part, I now have a need to have http://www.domain.com/pagename/idofsomething
I have made the changes relevant and the Controller is firing it. What I want to do is if the ID is passed to the Controller I need to get it to my RenderPartial call. I have got it working by setting the ID via ViewData but didn't know if that was the best approach?
Controller->ViewData->View->RenderPartial("Name",Model,ViewData)
In the actual UserControl can you read the action parameter or get it from ViewContext or similar?
In my RenderPartial I can then make a DB call to find the relevant item by the passed ID and then render relevant HTML from the DB object.
<% var id = ViewContext.RouteData.Values["Id"]; %>
SO - Getting the name of the controller and action method in the view in ASP.Net MVC ...

Resources