Script doesnt connect to JsonResult Controller

萝らか妹 提交于 2019-12-11 04:15:04

问题


I have a problem with connecting the script with the autocomplete function to my Json controller. The view is a formula, where the user can insert data, like dates with the datepicker function and general text to describe the issues. The whole formula is in this:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

All Textboxes, DropDownLists and Editors are connected to the model like so:

<div class="editor-label">
        @Html.LabelFor(model => model.Overview)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Overview)
        @Html.ValidationMessageFor(model => model.Overview)
    </div>

At the moment I try to insert the Textbox, where the autocomplete should happen like this:

 <b>Name: </b>
     @Html.TextBox("searchTerm", null, new { id = "txtSearch" })

The txtSearch is connected to my skript SearchUser.js:

$(function () {
    $("#txtSearch").autocomplete({
        source: '@url.Action("New1", "Dialog")',
        minLength: 1
    });
});

when I use a source an array of strings, the autocomplete appears.

the JavaScript is registered on top of the view and jQueryUI is registered in _Layout.cshtml. I am using jquery 1.11.3 and jqueryui 1.11.4 .

In The Controller New1 in the JsonResult you find this:

public JsonResult Dialog(string search)
{
    List<string> users = db
                            .Users
                            .Where(p => p.FirstName.ToLower().Contains(search.ToLower()))
                            .Select(p => p.LastName)
                            .ToList();

    return Json(users, JsonRequestBehavior.AllowGet);
}

when i test the website and look for http://localhost:51299/New1/Dialog?search=m i get the json file. The json file contains this: ["Mueller"]

But when I go to my formula http://localhost:51299/New1/Create and insert "m" into the TextBox nothing happens.

So now my question: What can i do to make it work?


Update (It's working!!!)

Aaaaah its working!!!. Thanks a lot! He couldnt use the source, so now I changed it to "/New1/Dialog". I know it is not a good way to use the direct url instead of '@url.Action("Dialog", "New1")', but i think he couldnt differ between normal ' and ". If you have an Idea why i couldnt use @url.Action, i would be interested in it.

View (Create.cshtml)

@Html.TextBox("searchTerm", null, new { id = "searchTerm" })

Skript (SearchUser.js)

$(function () {
$("#searchTerm").autocomplete({
    source: "/New1/Dialog",
    minLength: 1
});
}); 

controller (New1Controller.cs)

public JsonResult Dialog(string term)
    {
        List<string> users = db
                            .Users
                            .Where(p => p.LastName.ToLower().Contains(term.ToLower()))
                            .Select(x => x.LastName)
                            .ToList(); 

     return Json(users, JsonRequestBehavior.AllowGet);
    }

回答1:


jQueryUI autocomplete is using the name term (not search) to craft a request. In other words, when you type "m", it's sending the following request:

http://localhost:51299/New1/Dialog?term=m

You should be able to fix this by simply renaming the parameter:

public JsonResult Dialog(string term)


来源:https://stackoverflow.com/questions/31641641/script-doesnt-connect-to-jsonresult-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!