ASP.NET MVC和ASP.NET AJAX Toolkit AutoCompleteExtender

时间:2022-04-02 03:21:20

Can I make an ASP.NET AJAX AutoCompleteExtender use an ASP.NET MVC JsonResult rather than an ASMX Webservice?

我可以使ASP.NET AJAX AutoCompleteExtender使用ASP.NET MVC JsonResult而不是ASMX Web服务吗?

I've got an ASP.NET AJAX Toolkit AutoCompleteExtender on an ASP.NET MVC View. It uses an JsonResult type function in my MVC Controller.

我在ASP.NET MVC View上有一个ASP.NET AJAX Toolkit AutoCompleteExtender。它在我的MVC控制器中使用JsonResult类型函数。

ASP.NET MVC View:

ASP.NET MVC视图:

<form runat="server">
    <asp:ScriptManager
        ID="ScriptManager1"
        runat="server"
        EnableScriptGlobalization="true" />
    <ajaxToolkit:AutoCompleteExtender 
        ID="autoComplete1"
        runat="server" 
        TargetControlID="TextBox1"
        ServiceMethod="find"
        ServicePath="/thing"
        MinimumPrefixLength="1"
        CompletionInterval="1000"
        EnableCaching="true"
        CompletionSetCount="20"
        DelimiterCharacters=";"
        ShowOnlyCurrentWordInCompletionListItem="true" />
    <asp:TextBox
        ID="TextBox1"
        runat="server" />
</form>

ASP.NET MVC Controller:

ASP.NET MVC控制器:

<AcceptVerbs(HttpVerbs.Post)> _
Function Find(ByVal collection As FormCollection) As JsonResult
    Dim search As String = collection(0)
    Dim j As New JsonResult
    j.Data = ...
    Return j
End Function

This fails because collection.Count is 0. Also, Request.QueryString.Count is 0.

这会失败,因为collection.Count为0.此外,Request.QueryString.Count为0。

How do I pass the typed string to my Find() function?

如何将键入的字符串传递给我的Find()函数?

2 个解决方案

#1


3  

Sadly, the ASP.NET AJAX AutoComplete Extended requires a SOAP web service.

遗憾的是,ASP.NET AJAX AutoComplete Extended需要SOAP Web服务。

That said, the source is available on CodePlex, so you could probably modify it to take a JSON string.

也就是说,CodePlex上提供了源代码,因此您可以修改它以获取JSON字符串。

Personally, as I was using jQuery elsewhere on a site, I decided not to use the ASP.NET AJAX stuff and go with a jQuery plugin (jQuery.Autocomplete)

就个人而言,当我在网站的其他地方使用jQuery时,我决定不使用ASP.NET AJAX的东西并使用jQuery插件(jQuery.Autocomplete)

This had the additional benefits of:

这具有以下额外的好处:

  1. Accepting a JSON string.
  2. 接受JSON字符串。

  3. Only requiring 2 script references (jQuery and the AutoComplete script) rather than the 7 I needed for the ASP.NET AJAX option.
  4. 只需要2个脚本引用(jQuery和AutoComplete脚本)而不是ASP.NET AJAX选项所需的7个。

  5. I can easily get minified versions of both jQuery and AutoComplete, but the ClientSide only scripts in the ASP.NET AJAX download are non-minified (unless I've missed something).
  6. 我可以很容易地获得jQuery和AutoComplete的缩小版本,但ASP.NET AJAX下载中的ClientSide只有脚本是非缩小的(除非我错过了一些东西)。

Obviously if you're already loading the framework elsewhere for other things, then your mileage may vary.

显然,如果您已经将框架加载到其他地方,那么您的里程可能会有所不同。

#2


2  

If the idea of consuming an ASMX web service is an issue, another alternative is to configure the autocomplete control to call a page method, which can reside in the code behind of the page (or control) in question.

如果使用ASMX Web服务的想法是一个问题,另一种方法是配置自动完成控件来调用页面方法,该方法可以驻留在相关页面(或控件)后面的代码中。

An overview of how to set this up can be found here.

可在此处找到有关如何设置此内容的概述。

#1


3  

Sadly, the ASP.NET AJAX AutoComplete Extended requires a SOAP web service.

遗憾的是,ASP.NET AJAX AutoComplete Extended需要SOAP Web服务。

That said, the source is available on CodePlex, so you could probably modify it to take a JSON string.

也就是说,CodePlex上提供了源代码,因此您可以修改它以获取JSON字符串。

Personally, as I was using jQuery elsewhere on a site, I decided not to use the ASP.NET AJAX stuff and go with a jQuery plugin (jQuery.Autocomplete)

就个人而言,当我在网站的其他地方使用jQuery时,我决定不使用ASP.NET AJAX的东西并使用jQuery插件(jQuery.Autocomplete)

This had the additional benefits of:

这具有以下额外的好处:

  1. Accepting a JSON string.
  2. 接受JSON字符串。

  3. Only requiring 2 script references (jQuery and the AutoComplete script) rather than the 7 I needed for the ASP.NET AJAX option.
  4. 只需要2个脚本引用(jQuery和AutoComplete脚本)而不是ASP.NET AJAX选项所需的7个。

  5. I can easily get minified versions of both jQuery and AutoComplete, but the ClientSide only scripts in the ASP.NET AJAX download are non-minified (unless I've missed something).
  6. 我可以很容易地获得jQuery和AutoComplete的缩小版本,但ASP.NET AJAX下载中的ClientSide只有脚本是非缩小的(除非我错过了一些东西)。

Obviously if you're already loading the framework elsewhere for other things, then your mileage may vary.

显然,如果您已经将框架加载到其他地方,那么您的里程可能会有所不同。

#2


2  

If the idea of consuming an ASMX web service is an issue, another alternative is to configure the autocomplete control to call a page method, which can reside in the code behind of the page (or control) in question.

如果使用ASMX Web服务的想法是一个问题,另一种方法是配置自动完成控件来调用页面方法,该方法可以驻留在相关页面(或控件)后面的代码中。

An overview of how to set this up can be found here.

可在此处找到有关如何设置此内容的概述。