How to Refresh Base View from Partial View Action
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 b开发者_StackOverflow社区ase 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 actionIndex
- Inside the base view a call is made to render a partiaol view, via
Html.RenderPartial()
,Html.RenderAction()
orHtml.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 aJsonResult
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.
精彩评论