0

I am using ajax helpers to load data asynchronously, I am using MVC5, EF6 and returning PartialView from controller but partial view is loading on full page instead of updating a DIV asynchronously. Following is my code:

SampleDBContext db = new SampleDBContext();
public ActionResult Index()
{
    return View();
}

// Return all students
public PartialViewResult All()
{
    List<Student> model = db.Students.ToList();
    return PartialView("_Student", model);
}

my index.cshtml code is

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div style="font-family:Arial">

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
        <h2>Students</h2>

        @Ajax.ActionLink("All", "All",
    new AjaxOptions
    {
        HttpMethod = "GET", // HttpMethod to use, GET or POST
        UpdateTargetId = "divStudents", // ID of the HTML element to update
        InsertionMode = InsertionMode.Replace // Replace the existing contents
    })

        <br /><br />
        <div id="divStudents">
        </div>
    </div>

</body>
</html>

Markup of _Student.cshtml Partial view is:

@model IEnumerable<GS.MVC_Ajax_Venkat3.Models.Student>
<table class="table" style="border:1px solid black; background-color:greenyellow">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalMarks)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TotalMarks)
            </td>
        </tr>
    }
</table>

I have added Keys to web config also as follows

2
  • Are the scripts being loaded onto your page? Commented Dec 28, 2014 at 23:41
  • Thanks @Shoe I resolved it, it was some issues regarding script files. Commented Dec 29, 2014 at 3:25

1 Answer 1

0

The problem was that I referenced jquery.unobtrusive-ajax.js twice, both on Layout page and View page. I removed script reference from View and then it worked fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.