自动完成ASP.Net MVC问题

时间:2014-12-19 14:35:40

标签: jquery asp.net-mvc

首先,我使用默认的Site.master创建了新的MVC2(Visual Studio 2010)项目。
我有一个例子的问题。代码可以在这里找到: jQuery with MVC

Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"     
  Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
 <%asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" />
    <script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $('#VisitorDetail').hide();
    });
    $("#PassId").autocomplete({
        source: function (request, response) {
            var customer = new Array();
            $.ajax({
                async: false,
                cache: false,
                type: "POST",
                url: '<%= (Url.Action("Autocomplete", "Pocetna"))%>',
                data: { "term": request.term },
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        customer[i] = { label: data[i].Value, Id: data[i].Key };
                    }
                }
            });
            response(customer);
        },
        select: function (event, ui) {
            //fill selected customer details on form
            $.ajax({
                cache: false,
                async: false,
                type: "POST",
                url: '<%= (Url.Action("GetDetail", "Pocetna"))%>',
                data: { "id": ui.item.Id },

                success: function (data) {
                    $('#VisitorDetail').show();
                    $("#Id").html(data.id)
                    $("#Name").html(data.name)
                    $("#Mobile").html(data.mobile)
                    action = data.Action;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('Failed to retrieve states.');
                }
            });
        }
    });

</script>

 </asp:Content>

 <%asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div>
        <%: Html.Label("Enter Your name")%>
        <%: Html.TextBox("PassId")%>
        </div>


<div id="VisitorDetail">
                    <label>Id</label><div id="Id"></div>
                    <label>Name</label><div id="Name"></div>
                    <label>Mobile</label><div id="Mobile"></div>
</div>
</asp:Content>

我的Pocetna控制器

[HttpPost]
        public JsonResult Autocomplete(string term)
        {
            var result = new List<KeyValuePair<string, string>>();

            IList<SelectListItem> List = new List<SelectListItem>();
            List.Add(new SelectListItem { Text = "test1", Value = "0" });
            List.Add(new SelectListItem { Text = "test2", Value = "1" });
            List.Add(new SelectListItem { Text = "test3", Value = "2" });
            List.Add(new SelectListItem { Text = "test4", Value = "3" });

            foreach (var item in List)
            {

                result.Add(new KeyValuePair<string, string>(item.Value.ToString(), item.Text));


            }
            var result3 = result.Where(s => s.Value.ToLower().Contains(term.ToLower())).Select(w => w).ToList();
            return Json(result3, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetDetail(int id)
        {

            DemoModel model = new DemoModel();
            // select data by id here display static data;
            if (id == 0)
            {
                model.id = 1;
                model.name = "Yogesh Tyagi";
                model.mobile = "9460516787";

            }
            else
            {
                model.id = 2;
                model.name = "Pratham Tyagi";
                model.mobile = "9460516787";

            }

            return Json(model);

        }

的Site.Master

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

<asp:ContentPlaceHolder ID="HeadContent" runat="server" / >

</head>

<body>
    <div class="page">

        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>

            <div id="logindisplay">
                <% Html.RenderPartial("LogOnUserControl"); %>
            </div> 

            <div id="menucontainer">
            <nav>
                <ul id="menu" class="nav">              
                    <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
                    <li><%: Html.ActionLink("About", "About", "Home")%>
                        <ul>
                        <li> <%: Html.ActionLink("Autocomplete","Autocomplete","Home")%></li>
                        </ul>
                    </li>
                </ul>
            </nav>
            </div>
        </div>

        <div id="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />

            <div id="footer">
            </div>
        </div>
    </div>
</body>
</html>

没有Site.master的代码工作正常,但是当我使用默认的Site.master样式将代码复制到我的页面时它停止工作,我想知道是否有人有类似的问题。

我知道我可以创建一个普通的页面,但是因为我有一周没有丢失,所以如果有更好的解决方案,我很好奇。

0 个答案:

没有答案