ASP.Net MVC 3.0 Ajax.BeginForm重定向到一个页面?

时间:2011-11-25 20:16:13

标签: asp.net-mvc-3 ajax.beginform

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

并点击JsonResult 在表单的成功我称之为jQuery函数。 但由于某种原因,我的表单重定向到JsonAction

我的观点


@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" />
}

我的控制器


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

出于某种原因,当我点击提交时。 它运行JSonResult并将页面重定向到host / controller / actionname 我已经包含了我的

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

在我的layout.cshtml中

任何人都能告诉我可能出现的问题吗?

我发现了问题。现在我必须找到解决方案 提交 我正在验证我的表格

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

如果我排除了验证,Ajax表单按预期工作。 但是,如果我验证我的表单,那么ajax表单不能按预期工作 感谢

1 个答案:

答案 0 :(得分:18)

当发生这种情况时,几乎总是因为你的脚本文件未加载

来自:

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

  1. 在web.config中设置提到的标志:
    1. 包含对jQuery库的引用〜/ Scripts / jquery-1.4.4.js
    2. 在〜/ Scripts / jquery.unobtrusive-ajax.js
    3. 中包含一个引用该魔术的库的引用
  2. 因此加载fiddler http://fiddler2.com并查看是否正在调用和加载脚本。