How to update my View after Ajax Post

10,249

Solution 1

Ok May be this was very trivial but this did the trick.

In the OnSuccess of the AJAX Post I did put following and it worked:

 $("body").html(result);

Solution 2

If Your values are updated at database or you get the values at controller and you just need to add the following code in the success function of ajax call,,

location.reload(true);

this will work...

Share:
10,249
Lost
Author by

Lost

Updated on June 12, 2022

Comments

  • Lost
    Lost almost 2 years

    I am in MVC environment and I am calling one of my Controller methods from my View. Controller method subsequently does some validation check and returns to the same view with faulty modelstate

    At this point, I am expecting all of my validation fields which look like following:

    <td>@Html.ValidationMessageFor(m => m.Comments)</td>
    

    to light up with error messages. That is not happening. I think it is because I need to reload the view with new faulty Model. How can I do that?

    Following is snipped from my Ajax code:

    $("#Save").click(function () {
            var model = {
                ApplicationNumber: '@Model.ApplicationNumber',
                ApplicationId :'@Model.ApplicationId' ,
                Name: $('#Name').val(),
                CreateDate: $('#CreateDate').val(),
                OverrideHireDate: $('#OverrideHireDate').val(),
                Amount: $('#Amount').val(),
                VendorId: $('#Vendor').val(),
                Comments: $('#Comments').val(),
                CurrentState: '@Model.CurrentState',
                CurrentStatusDate: '@Model.CurrentStatusDate'
        };
            $.ajax({
                data: model,
                url: '@Url.Action("SaveApplication", "Applications")',
                type: "POST",
                success: function (result) {
                    $(function () {
                        // some code to activate validation controls?
                    });
                }
            });
        });
    

    Following is my controller:

    public ActionResult SaveApplication(ApplicationModel application)
            {
                if (!ModelState.IsValid)
                {
                   // ModelState.AddModelError("Name", "This is a Test");
                    return View("New",application);
                }
    
                ApplicationBLL.SaveApplication(application);
                return Content(string.Empty);
            }