How to Refresh Base View from Partial View Action - asp.net-mvc

I'm new to MVC, and I'm implementing a web app with lot of AJAX and partial views.
I have 2 views: 1 base view and 1 partial view. Each view has its own controller. The base view send data to the partial view via Model.
I'd like to make the partial view/controller send data to the base view/controller. What is the best way to do that? Is there a way to refresh the base view from a partial view action?
public ActionResult SendDataToBaseView()
{
return View("BaseView", viewModel);
}

It's not quite clear what your setup is.
If you're updating HTML that was rendered from the main view, then you'll likely have another controller action on the same controller on which you called an action to render the base view. This other controller action may return JSON or HTML (as a Partial View perhaps) and it will be your responsibility on the client side to take the result of the AJAX call and populate the sections of the page that were rendered by the base view with this new data.
Here are the steps that I would envisage being taken
Controller action called to render base view. Let's call the controller BaseController and the action Index
Inside the base view a call is made to render a partiaol view, via Html.RenderPartial(), Html.RenderAction() or Html.Action()
Response is sent to client
Event happens on client requiring update to data rendered by base view.
AJAX request is made to BaseController Update action that returns a JsonResult containing data that can be used to updates parts of the response outputted by the base view.
Receive response on client side and update those parts of the DOM.

Related

MVC - How to include a model inside _Layout for Partial View

I've seen how to use Partial Views from within a View and I understand how a model is passed from the View to a Partial View. What I don't grasp is how to include a Partial View inside of _Layout.cshtml so that I can pass it to the Partial View - or, how the Partial View itself can call a Controller Action to create a Model for use.
Specifically, I want to include a Partial View within _Layout.cshtml to display in the header of every page. The Partial View will display a custom setting in the User Profile. I can't think of any way to obtain the Model without making a Controller Action call - but how is this done in/for a Partial View from within _Layout.cshtml?
Is my only option of accessing a Controller Action to build my Partial View's required Model to use a jQuery call? Or is there another way?
The answer is by calling #Html.Action().
It sounds like you're on the right track.
The key is to try to not pass multiple models around, make models complicated, or use the ViewBag needlessly.
Instead, any time you want information on every page, call an action from your _Layout.
For example, you might have a controller called PartialsController or SharedController.
public class PartialsController : Controller
{
[ChildActionOnly]
public ActionResult UserProfilePartial()
{
UserProfileModel model = new UserProfileModel();
return PartialView("_UserProfile", model);
}
}
The ChildActionOnlyAttribute means that users cannot access the action directly. Only your code can call the action. You can also apply the attribute to the controller so that it affects all actions automatically.
Now call the action from your view (_Layout).
#Html.Action("UserProfilePartial", "Partials")

How to Load Content into an ASP.NET MVC Partial View in a Layout View

'm using ASP.NET MVC 3 with Razor views. I have a partial view which I would like to render on all pages so I am placing it in the site's main Layout page. However, I am not sure the best way to load data into the partial view. I could load it for each ActionMethod but there is there a way to do this globally across the entire app?
Move all the data loading logic for your partial into a separate action method.
Then in your layout page, instead of rendering the partial with a call to RenderPartial(), call the RenderAction() method.
RenderAction() makes a "child" action call - thus putting all the logic needed for that partial into one place.
Write action for this partial view in MasterController because every controller inherits from it, and place your partial view in shared folder and call it on Site's master page (like every site has a user control which gives login box until user is logged in else it displays logged in user info) ... hope it answer your ques ...

Call js from MVC Controller

I have a web site written using ASP.NET MVC3.
The site has one single web page and what I want to do is to have the controller handle the request, do some parsing and processing and thereafter trigger a certain javascript method in the single webpage based on part of the data from the request.
How would I do this in the MVC controller?
You can't directly trigger actions in the View from the controller.
However, what you can do is return an ActionResult from the Controller Action to the View that instructs the View to execute your Javascript.
This could be a PartialView that includes the Javascript (in which case, you'll need a placeholder in your view for the PartialView to be rendered 'into') or something like a JSONResult that contains a property instructing the logic within the View what to do.
Either way, the call to the Controller Action is going to be triggered by client-side Javascript and your desired logic executed when the Controller Action finishes executing.
You'll probably find it easier to use jQuery.

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.

How do I pass data from a controller to a strongly typed user control in asp.net mvc?

I have a strongly typed User Control which should show a user's orders on all pages, it's included in a master page. I have an OrdersController which can give me the current orders and is used at other locations.
How do I tell the UserControl in the Master Page that it should get its Data from that specific Controller / Controller Action? I'd like to access the viewdata within the ascx just as I would in a normal View.
Pass model and ViewData as parameters to the RenderPartial method.
It will make model and view data accessible as if you were in the parent view page.
<% Html.RenderPartial ( "../Shared/HRMasterData/DependentPersonDossiers",
ViewData.Model, ViewData ); %>
One way to do this would be to implement a base controller. Move the logic that obtains the orders to the base controller. Override OnActionExecuted in the base controller and check if the result is a ViewResult. If it is, find the orders and add them to the ViewData. In your master, check if the orders are present in the view data and, if so, render them. If not, show an "order data unavailable" message (this latter shouldn't happen, but it's best to be safe). Reuse (call) the code in the base controller in your OrdersController for the action that renders the orders view.

Resources