jquery.autocomplete - 路径中的非法字符

时间:2012-11-09 13:07:28

标签: jquery ajax asp.net-mvc-3 autocomplete

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

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

Autocomplete.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);
}

Ajax Response

2 个答案:

答案 0 :(得分:0)

ASP.NET MVC jQuery autocomplete with url.action helper in a script included in a page我相信解决了您的问题 - 您正在尝试访问非法字符:http://localhost:51395/UserManager/@Url.action(%20 ... etc

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

答案 1 :(得分:0)

window.location = '<%=@Url.Action("GetValue","TaskNew") %>';