Asp.net MVC partial view controller actions

I am very new to web application development, and I thought I would start with the latest technology, and therefore I am trying to learn asp.net in the same way as the MVC framework right away. This is probably a very simple question for you MVC professionals.

My question is, should the partial view have a related action, and if so, does this call when the normal page uses RenderPartial() on the partial view?

+72
asp.net-mvc asp.net-mvc-partialview
Sep 03 '09 at 1:36
source share
4 answers

While you may have an action that returns a partial view, you do not need an action to partially view it. RenderPartial takes a partial view and displays it using this model and displaying the data, if presented, in the current (parent) view.

You may need an action that returns a partial view if you use AJAX to load / reload part of the page. In this case, returning the full view is undesirable since you only want to reload part of the page. In this case, you can force the action to simply return the partial view corresponding to this section of the page.

Standard gear

Using partial view in normal mode (no action required)

 ...some html... <% Html.RenderPartial( "Partial", Model.PartialModel ); %> ...more html.. 

Ajax mechanism

Reloading part of a page using AJAX (a note that partially displays in the original page load)

 ...some html... <div id="partial"> <% Html.RenderPartial( "Partial", Model.PartialModel ); %> </div> ...more html... <script type="text/javascript"> $(function() { $('#someButton').click( function() { $.ajax({ url: '/controller/action', data: ...some data for action..., dataType: 'html', success: function(data) { $('#partial').html(data); }, ... }); }); }); </script> 

Controller for AJAX

 public ActionResult Action(...) { var model = ... ... if (Request.IsAjaxRequest()) { return PartialView( "Partial", model.PartialModel ); } else { return View( model ); } } 
+130
Sep 03 '09 at 1:41
source share

The accepted answer is absolutely correct, but I want to add that you can load a partial view using jQuery download. A smaller configuration is necessary if you do not want to consider concurrency.

 $("#Your-Container").load("/controller/action/id"); 
+3
Oct 02 '16 at 17:41
source share

I was able to achieve something similar with this logic.

Inside .cshtml

 @Html.Action("ActionMethodName", "ControllerName"); 

Inside the controller

 [Route("some-action")] public ActionResult ActionMethodName() { var someModel = new SomeModel(); ... return PartialView("SomeView.cshtml", someModel); } 

What is it.

If you need to pass values ​​from .cshtml to an action method, then this is possible.

+1
Aug 17 '17 at 13:33 on
source share

The answer is no. But sometimes you need some kind of controller action behind a partial view. Then you can create an actionMethod that returns a partial view. This actionMethod can be called in another view:

 @Html.Action("StockWarningsPartial", "Stores") 

The method of action may look like this:

 public ActionResult StockWarningsPartial() { .... return View("StockWarningsPartial", warnings); } 

and the view "StockWarningsPartial.cshtml" begins with:

 @{ Layout = null; } 

so that it does not display your surrounding layout again.

0
Jun 22 '17 at 8:22
source share



All Articles