Asp.net mvc partialview @ Ajax.ActionLink not working
I have a watch page
my browse page
<div id="beReplaced"> @Ajax.ActionLink("please click on me to bring the partial view", "PatrialViewToBeCalled", new AjaxOptions() {UpdateTargetId = "beReplaced", InsertionMode = InsertionMode.InsertAfter, HttpMethod="Get", LoadingElementId = "prgress" }) </div> I have a controller
public PartialViewResult PatrialViewToBeCalled() { var customer = db.Customers.First(); return PartialView("PartialViewThatMustBeShow",customer); } but when I click on the generated link, it takes me to a new page instead of replacing or adding a partial view to the div tag.
What is the problem?
I have found a solution. The problem arose due to a damaged unobtrusive ajax.min.js. file
You may have been absent:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> In the main window. This tells the main view to recognize the ajax helper.
If you have jQuery loaded into your page, why not use the simple get / load jquery method to get a partial view?
<div id="beReplaced"> <a href="#" id="lnk1">please click on me to bring the partial view</a> </div> Javascript
$("#lnk1").click(function(){ $.get('/controller/PatrialViewToBeCalled', function(data) { $('#beReplaced').html(data); }); }); If you are working with ASP.NET MVC 4, make sure that
@Scripts.Render("~/bundles/jquery") also located at the end of the body tag.
@Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false) </body> </html> I worked on this for too long, thinking it didn't work. Viewing the source of the page also showed nothing.
In the end, I found out that it really worked when I saw successful requests in the Firebug console. It just showed it off the screen. On the Firebug HTML tab, I finally found a way out, although it does not appear in the page source.