Ajax.BeginForm导致重定向到局部视图,而不是就地 [英] Ajax.BeginForm results in redirect to partial view instead of in-place

查看:145
本文介绍了Ajax.BeginForm导致重定向到局部视图,而不是就地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Search.cshtml有一个名为对搜索结果分区要被更新。 SearchResult所是动作的名称。我已经做了很多次的MVC2 / VS2008的项目,但是这是我第一次使用MVC3和VS2010。

My Search.cshtml has a div named "search-results" that is to be updated. SearchResults is the action name. I have done this many times on MVC2/VS2008 projects, but this is my first using MVC3 and VS2010.

现在的问题是,不是我的搜索结果在我的div被渲染,它点击提交被重定向我显示我的部分作为一个独立的页面。

The problem is, instead of my search result being rendered in my div, it clicking submit is redirecting me displaying my partial as a standalone page.

我已阅读,这可能是因为阿贾克斯未启用。我_Layout.cshtml看起来是这样的:

I have read that this may be because Ajax is not enabled. My _Layout.cshtml looks like this:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <script src="@Url.Content("~/Scripts/2011.2.712/jquery-1.5.1.min.js")"  type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.core.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet"  type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/main.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/2011.2.712/jquery-1.5.1.min.js")" type="text/javascript"></script>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.transparent.css").Combined(true).Compress(true))) 
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>

            @(Html.Telerik().Menu()
                    .Name("menu")
                    .Items(menu => {
                        menu.Add().Text("Home").Action("Index", "Home");
                        menu.Add().Text("About").Action("About", "Home");
                        menu.Add().Text("Employees").Action("List", "Employee");
                    }))
        </div>

        <div id="main">
            @RenderBody()
            <div id="footer">
            </div>
        </div>
    </div>
    @(Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Combined(true).Compress(true)))</body>
</html>

我是否需要添加MicrosoftMvcAjax.js或jquery.unobtrusive-ajax.js吗?我的web.config(根),则为如下:

Do I need to add MicrosoftMvcAjax.js or jquery.unobtrusive-ajax.js in? My web.config (root) contians the following:

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

如果我在添加MicrosoftMvcAjax.js在单元结束时,我得到一个错误sayning命名空间类型是MicrosoftMvcAjax.js的第一行未定义错误:

If I add MicrosoftMvcAjax.js in at the end of the element, I get an error sayning namespace 'Type' is undefined error from the first line of MicrosoftMvcAjax.js:

    Type.registerNamespace('Sys.Mvc');Sys.Mvc.$create_AjaxOptions=function(){return {};}

我缺少的是在这里。我相信我的code是好的,因为它复制alsmost逐字从我的MVC2项目。

What am I missing here. I am sure my code is fine, as it copied alsmost verbatim from my MVC2 projects.

推荐答案

您忘了包括 jquery.unobtrusive-ajax.js 脚本到您的网页(调整路径如需要):

You forgot to include the jquery.unobtrusive-ajax.js script to your page (adjust the path as necessary):

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

微软的* .js 脚本而言,他们是过时的ASP.NET MVC 3,应该不再被使用,除非你是移植了一些遗留的应用程序。它们已被替换的jQuery。

As far as Microsoft*.js scripts are concerned, they are obsolete in ASP.NET MVC 3 and should no longer be used unless you are porting some legacy application. They have been replaced by jQuery.

这篇关于Ajax.BeginForm导致重定向到局部视图,而不是就地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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