dvjanm
dvjanm

Reputation: 2381

How to load partial view after ajax post?

I have just started to use partial views in ASP.NET MVC4. It works fine when I click the submit button, the partial view loaded properly. I have to use DropDownLists and date fields which have to make ajax post when the selected index or value changes. (There are some other dropdownlists and those values are based on the selected date, and other selected values)

This is one of the fields:

@Html.EditorFor(model => model.Overtime.StartDate)

And this is the way I solved the AutoPostBack. I made myself the replacing of the html contents. But I thought that it can be made by the Framework even if the ajax post is not fired from the submit button.

<script type="text/javascript">
    $(document).ready(function () {
        $("#Overtime_StartDate").datepicker({ dateFormat: 'yy/mm/dd' });

        $("#Overtime_StartDate").change(function () {
            var postData = $("#PostData").serialize();
            $.post("ManageOvertimes", postData, function (data) {
                $('#Fields').html(data);
            });
        });
    });
</script>

The page that contains the partial view:

<div id="Fields">
    @Html.Partial("IndexPartial", Model)
</div>

So the question is: Is there a standard solution for this in ASP.NET MVC4?

Upvotes: 1

Views: 2337

Answers (1)

Andrey Gubal
Andrey Gubal

Reputation: 3479

I'm not sure I understand you properly. But if you want to load PartialView in div as a postback of $post function, you may use function load():

$.post("ManageOvertimes", postData, function () {
                $('#Fields').Load('@Url.Action("Actionname", "Controllername")')
            });

Upvotes: 1

Related Questions