Asynchronously passes the model-to-controller method using jquery and returns an error message asynchronously

Source: Internet
Author: User
Tags tojson

The model that needs to be passed through jquery to the controller method is:

     Public class Person
    {
         Public string Name {get; set;}
         Public int
    }

use the Message property of the following class to load a success or error message, and eventually convert it to JSON format to return to the view:

     Public class Personvm
    {
         Public string
    }

The controller displays error messages by traversing the Modelstate.keys.

       Public ActionResult Demo ()
        {
            return View ();
        }
        [HttpPost]
         Public ActionResult Saveperson (person person)
        {
            if (Modelstate.isvalid)
            {
                string string. Format ("user name: {0}, Age: {1}", Person.) Name, person. Age);
                return Json (new PERSONVM () {message = message});
            }
            Else
            {
                string "<div class=\" Validation-summary-errors\ "> The following error:<ul> occurred";
                foreach inch Modelstate.keys)
                {
                    var error = Modelstate[key]. Errors.firstordefault ();
                    if null)
                    {
                        "<li class=\" field-validation-error\ ">" +error. errormessage+"</li>";
                    }
                }
                "</ul>";
                return Json (new PERSONVM () {Message = errormessage});
            }
        }    

The view uses the $.tojson () method to convert the model to a JSON format and pass it to the Controller method. Convert the object into a JSON-formatted jquery extension, here.

@model MvcApplication1.Models.Person
@{
    "Demo";
    "~/views/shared/_layout.cshtml";
}
@Html. Editorformodel ()
<input type="Submit"value="Save" id="personcreate"/>
<div>
    <span id="Resultmessage"></span>
</div>
@section scripts
{
    <script src="~/scripts/json.js"></script>
    <script type="Text/javascript">
        $ (function () {
            $ (' #personCreate '). Click (function () {
                var person = Getperson ();
                if null) {
                    Alert ("user name cannot be empty");
                    return;
                }
                var json = $.tojson (person);
                $.ajax ({
                    ' @Url. Action ("Saveperson", "Home") ',
                    ' POST ',
                    ' json ',
                    Data:json,
                    ' Application/json; Charset=utf-8 ',
                    Success:function (data) {
                        var message = data. Message;
                        $ ("#resultMessage"). HTML (message);
                    }
                });
            });
        });
        function Getperson () {
            var name = $ (' #Name '). Val ();
            var age = $ (' #Age '). Val ();
            return "" null : {name:name, age:age};
        }
    </script>
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.