asp.net mcv从动作返回对象,并在ajax成功中使用它

时间:2017-03-19 23:20:43

标签: javascript jquery ajax asp.net-mvc

我有一个ResultOfOperation类,用于获取有关活动的详细信息:

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

                        toastr.success(data.message1);

                    }
                });
            }

那个Jquery函数,从提交表单中调用:

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

一些控制器动作:

<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>

我是这样的:

def sorting(tup1, tup2):
    output = []
    sumVal = 0
    wholeTup = tup1 + tup2 
    for i in range(0, len(wholeTup)):
      if i % 2 == 0 or i == 0:
        word = wholeTup[i].title()
        output.append(word)
        output.sort()     
      elif i % 2 != 0:
        sumVal = sumVal + wholeTup[i] 
    return output

print(sorting(("Bob",21,"kelly",21), ("morgan",10,"Anna",2)))

我的问题是我如何使用我的返回对象并在toastr.js中显示它?

1 个答案:

答案 0 :(得分:1)

您的方法需要返回JsonResult

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