Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load partial view into div

I'm trying to load a partial view into a div from a calling view, however the partial view loads into a new page. There isn't much code to it and I've tried various ways from other posts, but it still loads into a new page. So I think I might be missing something fundamental.

Controller

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
    return View();
}

public ActionResult Test()
{
    return PartialView("Test");
}

View

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

@using (Ajax.BeginForm("Test", "Home", new AjaxOptions { InsertionMode=InsertionMode.Replace, UpdateTargetId = "mydiv" }))
{    
    @Ajax.ActionLink("Save", "Test", "Home", new AjaxOptions{ });
}

<div id="mydiv">
    @Html.Partial("Test")
</div>

PartialView

<h1>Test</h1>
like image 411
user415394 Avatar asked Dec 29 '10 19:12

user415394


People also ask

How do I load partial views?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

How do you pass a partial view model?

To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.

Can you just update a partial view instead of full page post?

The Partial View will be updated with the partial view returned from the child action. Jquery is still a legitimate way to update a partial. But technically, the answer to your question is YES.

Can we return partial view in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.


2 Answers

Couple of remarks about your code:

  • Ajax.* helpers in ASP.NET MVC 3 RC2 no longer use MS AJAX which has been deprecated in favor of jQuery, so your script inclusions are wrong. You need to include jquery
  • You have an ajax form without a submit button and an ajax link inside this form. This makes no sense. Either use an ajax form or an ajax link.

So your code might look something among the lines:

<script src="@Url.Content("~/scripts/jquery-1.4.4.js")" type="text/javascript"></script>

@Ajax.ActionLink("Save", "Test", "Home", new AjaxOptions { 
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "mydiv"
})

<div id="mydiv">
    @Html.Partial("Test")
</div>
like image 149
Darin Dimitrov Avatar answered Nov 06 '22 02:11

Darin Dimitrov


Add this line to your page (master page or layout)

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

just after MicrosoftMvcAjax.js line.

like image 26
RezaRa Avatar answered Nov 06 '22 03:11

RezaRa