ASP.Net MVC 3.0 Ajax.BeginForm被重定向到一个页面? [英] ASP.Net MVC 3.0 Ajax.BeginForm is redirecting to a Page?

查看:333
本文介绍了ASP.Net MVC 3.0 Ajax.BeginForm被重定向到一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.Net MVC 3.0我使用的是Ajax.Beginform

和创JsonResult
在窗体的成功我打电话jQuery的功能。
但由于某些原因,我的形式重定向到JsonAction

我的视图


  @using(Ajax.BeginForm(ActionName,控制器,空,新AjaxOptions
           {
               列举HTTPMethod =POST
               的onSuccess =ShowResult
           },{新的id =MyForm的}))
{
    //所有表单域
    <输入类型=提交值=继续级=按钮标准/>
}

我的控制器


 公共JsonResult ActionName(的FormCollection集合)
{
    返回JSON(新{} _status,JsonRequestBehavior.AllowGet);
}

的jQuery


 <脚本类型=文/ JavaScript的>
功能ShowResult(数据){
   //警报(我在ShowResult);
    如果(data.isRedirect){
        window.location.href = json.redirectUrl;
    }
}

由于某些原因,当我点击提交。
它运行JSonResult并重定向页面到主机/控制器/ actionname
我已经包括了我的

 <脚本的src =@ Url.Content(jquery.unobtrusive-ajax.min.js)>< / SCRIPT>

在我的layout.cshtml

任何一个可以告诉我是什么地方出错了?

我发现这个问题。现在我已经找到解决方案
上提交
我验证我的形式

  $(#MyForm的)。验证({
    submitHandler:功能(形式){
   //我的逻辑到这里....
 }});

如果我排除验证Ajax的形式按预期工作。
但是,如果我验证我的形式,然后阿贾克斯形式没有按预期工作
谢谢


解决方案

在这种情况下它几乎总是因为你的脚本文件没有加载

这注:

<一个href=\"http://completedevelopment.blogspot.com/2011/02/unobstrusive-javascript-in-mvc-3-helps.html\">http://completedevelopment.blogspot.com/2011/02/unobstrusive-javascript-in-mvc-3-helps.html


  1. 设置在web.config中提到的标志:

    1. 包括jQuery库〜/脚本/ jQuery的-1.4.4.js
    2. 参考
    3. 包含到库中的引用,在〜/脚本钩这个魔术/ jquery.unobtrusive-ajax.js


所以装载小提琴手 http://fiddler2.com ,看看脚本调用和加载。

In ASP.Net MVC 3.0 i am using a Ajax.Beginform

and hitting a JsonResult on success of the form i am calling a jQuery Function. but for some reason my form is redirecting to JsonAction

my View


@using (Ajax.BeginForm("ActionName", "Controller", null, new AjaxOptions
           {
               HttpMethod = "POST",
               OnSuccess = "ShowResult"
           }, new { id = "myform" }))
{
    // All form Fields
    <input type="submit" value="Continue" class="button standard" />
}

My controller


public JsonResult ActionName(FormCollection collection)
{
    return Json(new { _status },JsonRequestBehavior.AllowGet);
}

jQuery


<script type="text/javascript">
function ShowResult(data) {
   // alert("I am at ShowResult");
    if (data.isRedirect) {
        window.location.href = json.redirectUrl;
    }
}

for some reason, when i click submit. it runs the JSonResult and redirects the page to host/controller/actionname I have included my

<script src="@Url.Content("jquery.unobtrusive-ajax.min.js")"></script>

in my layout.cshtml

can any one tell me what could be wrong?

I found the problem. Now i have to find the solution on submit I am validating my form

$("#myform").validate({
    submitHandler: function (form) {
   // my logic goes here....
 }});

If i exclude the validation Ajax form works as expected. But if i validate my form then ajax form is not working as expected Thanks

解决方案

when this happens its almost always because your script files aren't loaded

note from:

http://completedevelopment.blogspot.com/2011/02/unobstrusive-javascript-in-mvc-3-helps.html

  1. Set the mentioned flag in the web.config:

    1. Include a reference to the jQuery library ~/Scripts/jquery-1.4.4.js
    2. Include a reference to the library that hooks this magic at ~/Scripts/jquery.unobtrusive-ajax.js

So load up fiddler http://fiddler2.com and see if the scripts are being called and loaded.

这篇关于ASP.Net MVC 3.0 Ajax.BeginForm被重定向到一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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