1

オートコンプリートが機能しています。私の検索方法は HomeController にありますが、いくつか変更を加えたいと思っています。「Vacancy」という名前の API コントローラーを作成し、そこに Search メソッドを転送しましたが、動作させることができません: Status Code:404 Not Found (Search method も開始されません)。私が行ったすべての変更を転送した後は次のとおりです。

ビューのソース リンクを変更:

data-autocomplete="@Url.Action("Search", "Vacancy")"

変更された検索方法:

public object Search(string term)
    {
        var vacancyHeaders =
            new UnitOfWork().Repository<Vacancy>()
                            .Get()
                            .Where(v => v.Header.ToLower().Contains(term.ToLower()))
                            .Select(v => new { label = v.Header })
                            .Distinct()
                            .Take(10);
        return vacancyHeaders;
    }

助けてください、なぜ私の検索方法が始まらないのですか? 以下は、APIコントローラーのない私の作業コードです:

意見:

<form data-bind="submit: search">
        <input data-bind="value: SearchArgument, valueUpdate: 'blur'" data-autocomplete="@Url.Action("Search", "Home")"  class="form-text" name="search" size="32" maxlength="64" placeholder="Search"/>
        <input type="submit" value="search" />
</form>

脚本

 $(":input[data-autocomplete]").each(function() {
                $(this).autocomplete({ source: $(this).attr("data-autocomplete")});

            });

検索方法

 public ActionResult Search(string term)
    {
        var vacancyHeaders =
            new UnitOfWork().Repository<Vacancy>()
                            .Get()
                            .Where(v => v.Header.ToLower().Contains(term.ToLower()))
                            .Select(v => new { label = v.Header })
                            .Distinct()
                            .Take(10);
        return Json(vacancyHeaders, JsonRequestBehavior.AllowGet);
    }
4

1 に答える 1

0

Web API でカスタム メソッド名を使用する場合は、ルート構成を変更して許可する必要があります。

ASP.NET Web APIのカスタム メソッド名の質問から、追加の GET メソッドを組み込み、通常の REST メソッドをサポートするように WebApiConfig ファイルを構成する方法の例を次に示します。

config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
config.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

これで、検索方法を次のようにすることができます。

public IQueryable GetAutocompleteResponse(string term = null)
{
    var someStrings = new string[] { "vacancy1", "vacancy2" };

    var model = someStrings.Select(c => new { label = c }).AsQueryable();

    return model;
}

最後に、jQueryUI オートコンプリートを使用して、テストに使用したコードを次に示します。

<input type="search" name="searchTerm" data-autocomplete="@Url.Action("GetAutocompleteResponse", "Api/Vacancy")" />
<input type="submit" id="submitForm" value="Search By Name" />


@section scripts
{
    <script>
        var createAutocomplete = function () {
            var self = $(this);
            var options = {
                source: self.attr("data-autocomplete")
            };
            self.autocomplete(options);
        }

        $(document).ready(function () {
            $("input[data-autocomplete]").each(createAutocomplete);
        });
    </script>
}
于 2014-03-27T16:28:55.687 に答える