简体   繁体   中英

asp.net mcv return object from action, and use it in ajax success

I'm have that ResultOfOperation class that i use to get details about the activity:

public class ResultOfOperation
    {

        public string Message1 { get; set; }
        public string Message2 { get; set; }
        public string Message3 { get; set; }

    }

And that Jquery function, called from the form on submit:

function SubmitForm() {
                $.ajax({
                    url: '/Home/BindingTest',
                    data: $("#PersonForm").serialize(),
                    type: 'POST',
                    success: function (data) {

                        toastr.success(data.message1);

                    }
                });
            }

And some controller action:

public ResultOfOperation BindingTest(Person p)
        {
            //some DB operations.
            var rop = new ResultOfOperation();
            rop.Message1 = "Operation Success";
            return rop;
        }

My is like this:

<form id="PersonForm" action="@Url.Action("BindingTest", "Home")" method="post">
            <input type="text" name="firstName" />
            <br />
            <input type="text" name="lastName" />
            <br />
            <input type="number" name="age" />
            <br />
            <input type="submit" value="Submit" />
        </form>

My question is how can i use my returning object and show it in toastr.js for example?

Your method needs to return a JsonResult

[HttpPost]
public JsonResult BindingTest(Person p)
{
    //some DB operations.
    var rop = new ResultOfOperation();
    rop.Message1 = "Operation Success";
    return Json(rop);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM