1

I've faced a problem for partial view displaying.

    //This is Controller Action 
//_AcademicInfo is the Partial view name
public PartialViewResult GetAcademicInfo(int empId)
        {
            var acad = _academicService.GetAcademinByEmp(empId);
            return PartialView("_AcademicInfo", acad);
        }

<!--Parent/caller page cshtml code-->

@Ajax.ActionLink(
            "Academic Details",
            "GetAcademicInfo",
            "Employee", new {empId = Model.Id},
            new AjaxOptions {UpdateTargetId = "AcademicDetails", InsertionMode = InsertionMode.InsertAfter}
            )
 <!--This is partial view cshtml-->
@model IEnumerable<Hik.PhoneBook.Data.Entities.Academic>

<div id="result">

    <table>
        <tr>
            <th>Degree Name</th>
            <th>Passing Year</th>
            <th>CGPA</th>
            <th>Institute</th>
        </tr>


        @foreach (var acad in Model)
        {
            <tr>
                <td>@Html.DisplayFor(m => acad.DegreeName)</td>
                <td>@Html.DisplayFor(m => acad.PassingYear)</td>
                <td>@Html.DisplayFor(m => acad.CGPA)</td>
                <td>@Html.DisplayFor(m => acad.Institute)</td>
            </tr>
        }

    </table>

</div>

As because, I need to get the partial view by a Link clicking, not by rendering directly like @Html.Partial("_AcademicInfo", Model.Academic). Thats why I've used Ajax.ActionLink. Whenever I click on "Academic Details" link, its executing the accurate result. But unfortunately its not displaying in the same page. Its going to appear in another page. (Its MVC 4)

What should I need to change to render the partial view in the same page?

7
  • 1
    That because you have not included the relevant scripts - you need to include jquery.unobtrusive-ajax.js Commented Apr 24, 2015 at 6:54
  • Yup I've used that already @section scripts { <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> } Commented Apr 24, 2015 at 6:59
  • Then you possibly have duplicates or other scripts out of order (jquery needs to be first). Edit your question to show all the scripts in the view Commented Apr 24, 2015 at 7:06
  • @sharmoon You'll also need to provide either a more complete example of the "parent" view, or include the rendered HTML. Commented Apr 24, 2015 at 7:19
  • @StephenMuecke yup u r corrcet. My "jquery.unobtrusive-ajax.js" was set before main jQuery loading. But I cant mark your answer as correct answer coz there is no 'Right/Correct' sign besides ur answer. Commented Apr 24, 2015 at 10:02

3 Answers 3

3

If you are redirecting to a new page rather than staying on the same page, it means that jquery.unobtrusive-ajax.js is not loaded and @Ajax.ActionLink() falls back to a normal link.

Either you have either

  1. not included the script
  2. have the scripts in the wrong order (jquery{version}.js must come first)
  3. You have duplicate scripts
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you can use jquery's .load() function ? Like this.

$("divId").load('@url.action("getacademicInfo")', {empId : 4});

https://api.jquery.com/load/

Comments

0

Try using JQuery Ajax to load the partial view into the Div on the page on button/link click instead.

var jsondata = { Id: iddata };

$.ajax({
    url: "/Controllername/GetAcademicInfo",
    method: "POST",
    dataType: "html",
    data: jsondata,
    success: function (response) {
        $("#divId").html(response);

    }

});

Please note that this is just a sample code for giving you an idea.

Hope that helps!

1 Comment

OP is returning a partial view, not json, so it need to be dataType: "html",

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.