MVC4 Ajax forms: how to display server side confirmation

I use a @Ajax.BeginFormhelper method that submits form data to an Ajax server. When data arrives at the server, some server-side checks are processed, and the final result is sent back to the browser.

My problem: I want errors to appear without refreshing the page. I found a lot of questions on this, but they are old. I am wondering if there is any new way to achieve this. The best way I've found so far is to process the result using jQuery . But maybe there are some built-in functions in the new MVC4, how best to solve this problem?

+5
source share
1 answer

:

<div id="update-this">
@using (Ajax.BeginForm("YourAction", new AjaxOptions { UpdateTargetId = 'update-this' }))
{    
}
</div>

@Html.ValidationMessageFor(model => model.someField) , .

, - :

public ActionResult YourAction(YourModel yourmodel)
{
    if (ModelState.IsValid)
    {
        // Do what is needed if the data is valid and return something
    }
    return PartialView("DisplayPartial", yourmodel);
}

, . ( .)

+2

All Articles