我怎样才能返回JSON结果到Ajax.BeginForm [英] How can I return a JSON result to a Ajax.BeginForm

查看:111
本文介绍了我怎样才能返回JSON结果到Ajax.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种简单的形式:

  @using(Ajax.BeginForm(CreateProductFromAjaxForm,产品,
                  空值,
                  新AjaxOptions(){HttpMethod =后,的onSuccess =的getResult},
                  空值))
{
    @ Html.EditorFor(米=>米)
    <小时/>
    < D​​IV CLASS =形组>
        < D​​IV CLASS =COL-MD-偏移2 COL-MD-10>
            <输入类型=提交级=BTN BTN-INFO值=下一步/>
        < / DIV>
    < / DIV>
}
 

和,进行测试,一个简单的控制器:

  [HttpPost]
    公共JsonResult CreateProductFromAjaxForm(CreateProductModel模型)
    {
        如果(!ModelState.IsValid)
        {
            返回新JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                数据=新{结果=错误}
            };
        }

       //添加到数据库

        返回新JsonResult()
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            数据=新{结果=成功}
        };
    }
 

我已经添加了这些到Web.config:

 <添加键=ClientValidationEnabled值=真/>
 <添加键=UnobtrusiveJavaScriptEnabled值=真/>
 

和这些给我的脚本包:

  bundles.Add(新ScriptBundle(〜/包/ jQuery的)。包括(
                    〜/脚本/ jquery- {}版本.js文件,
                    〜/脚本/ jquery.validate.js
                    〜/脚本/ jquery.validate.unobtrusive.js
                    ));
 

点击提交按钮后,结果页面只显示:

  {结果:成功}
 

我希望,我能处理的结果,在的onSuccess =的getResult的处理程序,但它似乎并没有工作。

基本上我现在用的是 Ajax.BeginForm 主要用于客户方的验证。

你能告诉我什么是错?

更新:我添加了 HttpMethod =发帖的AjaxOptions。

更新:的getResult ,上面定义的 Ajax.BeginForm 是这样的:

 <脚本类型=文/ JavaScript的>
    VAR的getResult =功能(数据){
        警报(data.result);
    };
< / SCRIPT>
 

解决方案

您需要包括 jquery.unobtrusive-ajax.js 文件。

 <脚本的src =〜/脚本/ jquery.unobtrusive-ajax.js>< / SCRIPT>
 

I have got this simple form:

@using (Ajax.BeginForm("CreateProductFromAjaxForm","Product" , 
                  null, 
                  new AjaxOptions() {  HttpMethod = "post", OnSuccess = "getresult" },
                  null))
{
    @Html.EditorFor(m => m)
    <hr />
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-info" value="Next" />
        </div>
    </div>
}  

And, for testing, a simple controller:

    [HttpPost]
    public JsonResult CreateProductFromAjaxForm(CreateProductModel model)
    {
        if (!ModelState.IsValid)
        {
            return new JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new { result = "error" }
            };
        }

       //add to database

        return new JsonResult()
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new { result = "success"}
        };
    }  

I have added these to the Web.Config:

 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />

and these to my script bundle:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.unobtrusive.js"
                    ));

After clicking the "Submit" button, the resulting page just shows:

{"result":"success"}

I would expect that I could handle the result in the OnSuccess="getresult" handler, but it doesn't seem to work.

Basically I am using the Ajax.BeginForm mainly for the clientside validation.

Can you tell me whats wrong?

UPDATE: I added the HttpMethod = "post" to the AjaxOptions.

UPDATE: The getresult, is defined above the Ajax.BeginForm like this:

<script type="text/javascript">
    var getresult = function (data) {
        alert(data.result);
    };
</script> 

解决方案

you need to include jquery.unobtrusive-ajax.js file.

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

这篇关于我怎样才能返回JSON结果到Ajax.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆