asp.net - How to return errors or notifications from JSON Action method to Ajax forms? -
Assume that we have a standard Ajax form with the following syntax:
@ Using (Ajax.BeginForm ("AddCityJson", "City", New AjaxOptions {HttpMethod = "post", OnSuccess = "JScriptOnSuccess ();", OnFailure = "JScriptOnFailure"}))) {... form field here. .} A simple form that gathers some information about a city, submits and saves it.
We get the information on the controller side if everything goes well then we correct the on-the -space AjaxOption and if unsuccessful false ones are returned: < Pre> public JsonResult AddCityJson (form collection selection) {var city = new city ... ... populate in the field from the field in the archive} var cityRepository = new city repository; City = City Repository SaveArudit (city); If (city == faucet) {back Jason (false)} returns Jason (true); } I need to add various tests inside the controller AddCityJson method. Probably to check that the name of the city already exists or any other validation and if I have any errors or If the warning came back to the UI.
If I can pass any error / warning message to the UI, Ajax is expected to return the Ajax form right or wrong and whether this post was successful?
I would like to avoid using vendetta, viewbags. Thank you.
You can return any value to your JSON result, for example: < Former> returns Jason (new {success = false, message = error message}); And then explain it on the client side. There is no need to use ViewData, ViewBags etc. Just format the results on the client side, so you're just transmitting data to JSON.
EDIT: I did not know that you are using Ajax. You must still be able to hook JSON results in the client side to parse the BENEFER () results. See.
Although I suspect that most people will use jQuery only to accomplish the same thing, as described in various stack overflow positions such as the reaction of the drin. Whatever I would recommend, this is because the jQuery library is very strong and the cross platform has been tested.
Comments
Post a Comment