Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Ajax for partial view loading in .NET MVC4

I'm trying to follow the post here, which may very well be wrong, to learn a little more about partial view loading in MVC. I know the basics of MVC but want to do more of the ajax stuff I used to do before I started using MVC.

The goal is to have the partial view load INSIDE the div. Its just loading the partial view as the whole page, rather than inside of the Div.

Code is as follows:

Controller:

namespace LDP.WebUI.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }
        public PartialViewResult Page1()
        {
            return PartialView("Page1");
        }
        public PartialViewResult Page2()
        {
            return PartialView("Page2");
        }
    }
}

Main View (Index): (I also tried "mainDiv" without the pound sign, wasen't sure which was right)

<script src="@Url.Content("~/Scripts/libs/jquery-1.8.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/libs/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#hideme').click(function () {
            $(this).hide();
        });
    });
</script>
<div>
@Ajax.ActionLink("Parial Page1", "Page1", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "mainDiv"})
@Ajax.ActionLink("Parial Page2", "Page2", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "mainDiv"})
<div id ='mainDiv'>

</div>
<button id ="hideme">Hide Me</button>
</div>

Partial View 1 (Page1.cshtml)

@{
    ViewBag.Title = "Page1";
}

<h2>Page1</h2>

Partial View 2 (Page2.cshtml)

@{
    ViewBag.Title = "Page2";
}

<h2>Page2</h2>

Update: I created a brand new MVC 4 project. This comes, by default, with Jquery 1.7.1 and the unobtrusive-ajax library from microsoft. It still loads the partial view in a new page...

Update: Not sure if this helps, but the following does achieve the result I want... But still doesn't solve the problem as to why this solution doesn't work

<button>load about</button>
<script type="text/javascript">
    $("button").click(function () {
        $("#mainDiv").load("Test/Page2");
    });

</script>
    enter code here
like image 783
kralco626 Avatar asked Jan 25 '13 20:01

kralco626


People also ask

How do I return partial view in Ajax?

So, let's create one simple MVC application and try to return a partial from controller and display it using jQuery AJAX. Here is our small controller class. The controller class is just a stub and not doing anything great. It contains a testPartial() function that will return a partial view as a result.

How do I call ActionResult from Ajax?

You need to remove [HttpPost] attribute. You need to be aware of ajax parameters contentType and dataType. From the documentation: contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8'). This specifies what type of data you're sending to the server.

How do I render partial view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


2 Answers

  1. ditch # in UpdateTargetId = "#mainDiv"
  2. make sure you have jquery.unobtrusive-ajax.min.js script referenced on your page.
like image 71
Dmitry Efimenko Avatar answered Sep 28 '22 15:09

Dmitry Efimenko


I copied your code into a brand new MVC4 project and it works fine.

See screenshot of the solution:

Brand new MVC4 project with Ajax.ActionLink

Hope this will give you a hint on what could be wrong.

like image 44
Eric Eijkelenboom Avatar answered Sep 28 '22 15:09

Eric Eijkelenboom