Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update my View after Ajax Post

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);
        }
like image 544
Lost Avatar asked Jan 07 '23 12:01

Lost


1 Answers

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);
like image 108
Lost Avatar answered Jan 21 '23 03:01

Lost