小编典典

Ajax.BeginForm导致重定向到局部视图而不是就地

ajax

我的Search.cshtml有一个名为“ search-results”的div,该div将被更新。SearchResults是操作名称。我已经在MVC2
/ VS2008项目上做了很多次,但这是我第一次使用MVC3和VS2010。

问题是,单击提交将重新定向我的部分作为独立页面,而不是在div中呈现我的搜索结果。

我已经读到这可能是因为未启用Ajax。我的_Layout.cshtml看起来像这样:

<!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(根)包含以下内容:

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

如果我在元素末尾添加MicrosoftMvcAjax.js,则会收到一条错误消息,指出从MicrosoftMvcAjax.js的第一行开始,名称空间“
Type”是未定义的错误:

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

我在这里想念什么。我确信我的代码很好,因为它几乎逐字地从我的MVC2项目中复制了代码。


阅读 206

收藏
2020-07-26

共1个答案

小编典典

您忘记了将jquery.unobtrusive-ajax.js脚本包含到页面中(根据需要调整路径):

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

Microsoft*.js脚本而言,它们在ASP.NET MVC
3中已过时,除非您要移植某些旧版应用程序,否则不应再使用它们。它们已被jQuery取代。

2020-07-26