0

我正在使用 jquery.autocomplete 插件,当它发出 Ajax 请求时,它给了我 HTTP 错误代码 500,并且消息正文显示路径中的非法字符。

我正在使用 MVC3,并且我已将 javascript 代码从视图移动到一个单独的脚本文件,该脚本文件在我的 _layout.cshtml 文件中引用。在我移动我的 javascript 之前它可以工作,但现在它没有,我不明白为什么。

自动完成.js

$(document).ready(function () {
    alert("one");
    $("#group_name").autocomplete('@Url.Action("LookUpGroupName", "UserManager")', // Call LookUpGroupName ActionResult in UserManager Controller
        {
        dataType: 'jsonp',
        parse: function (data) {
            var rows = new Array();
            for (var i = 0; i < data.length; i++) {
                rows[i] = {
                    data: data[i],
                    value: data[i].group,
                    result: data[i].group
                }
            }
            return rows;
        },
        formatItem: function (row, i, max) {
            return row.group;
        },
        width: 300,
        highlight: false,
        multiple: false
    }); // End of autocomplete
    alert("two");
});

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.autocomplete.custom.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Custom/autocomplete.js")" type="text/javascript"></script>
  <link href="@Url.Content("~/Content/themes/base/jquery.ui.autocomplete.css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="page">
        <header>
           <div id="title" >
    <h1 style="color:Black">ALF and BRAD User Manager</h1>
</div>
<div id="logindisplay">
    <span style="color:Black; background-color:white; border-color:Black; border-style:solid; border-width:thin; text-align:right;">
        Logged in as:&nbsp<strong>@Context.User.Identity.Name</strong>
    </span> 
</div>

            <nav>
                <ul id="menu">
                    @Html.ActionLink("Home", "Index", "Home")&nbsp&nbsp
                    @Html.ActionLink("User Management", "Index", "UserManager")&nbsp&nbsp
                    @Html.ActionLink("User Status", "Index", "UserStatus")&nbsp&nbsp
                    @Html.ActionLink("Email Distibution List", "Index", "EmailDistributionList")&nbsp&nbsp
                    @Html.ActionLink("Email User Details", "Index", "EmailUserDetails")&nbsp&nbsp
                </ul>
            </nav>
        </header>
        <section id="main">
            @RenderBody()
        </section>
        <footer>
        </footer>
    </div>
</body>
</html>

看法

<!-- Declare model to be used for view -->
@model UserManager.Models.vw_UserManager_Model_Add_Users 
@{
    ViewBag.Title = "Create New User";
}
<h2>
    CreateUser</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>New User Details</legend>
        <div class="editor-label">
            @Html.LabelFor(Model => Model.salutation)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => Model.salutation)
            @Html.ValidationMessageFor(model => Model.salutation)
        </div>
       @{Html.Partial("~/Views/Partial/_AutocompleteGroupName.cshtml", this.ViewData);
}

阿贾克斯响应

4

2 回答 2

0
window.location = '<%=@Url.Action("GetValue","TaskNew") %>';
于 2013-09-22T08:22:46.673 回答
0

ASP.NET MVC jQuery 自动完成与包含在页面中的脚本中的 url.action 助手我相信可以解决您的问题 - 您正在获取非法字符,因为它试图转到:http://localhost:51395/UserManager/@Url.action(%20 ... etc

当文件被移动到外部脚本时,@Url.action 没有被解析,因为 javascript 没有通过该解析引擎运行,因为它是服务器端。

于 2012-11-09T13:24:27.517 回答