ASP.Net MVC 项目中的 Ajax 自动完成 - 如何显示对象的名称但实际保存它的 ID?
我已经在我的应用程序中使用查询数据库的 Web 服务文件实现了 Ajax 自动完成功能,效果非常好。我遇到的一个问题是允许用户查看项目的名称,因为这就是他们在文本框中输入的内容,但是当他们选择它时,它会保存项目的 ID 号而不是实际名称。我希望它的行为很像一个下拉列表,我可以在其中指定所看到和输入的内容与数据库中实际保存的内容(在本例中,是产品 ID 而不是它的名称。
)视图以及脚本:
<script type="text/javascript">
Sys.Application.add_init(function() {
$create(
AjaxControlToolkit.AutoCompleteBehavior, {
serviceMethod: 'ProductSearch',
servicePath: '/ProductService.asmx',
minimumPrefixLength: 1,
completionSetCount: 10
},
null,
null,
$get('ProductID'))
});
</script>
<p>
<label for="ProductID">Product:</label>
<%= Html.TextBox("ProductID", Model.Products)%>
<%= Html.ValidationMessage("ProductID", "*")%>
</p>
这是我的 asmx 文件中的内容:
public class ProductService : System.Web.Services.WebService
{
[WebMethod]
public string[] ProductSearch(string prefixText, int count)
{
MyDataContext db = new MyDataContext();
string[] products = (from product in db.Products
where product.ProductName.StartsWith(prefixText)
select product.ProductName).Take(count).ToArray();
return products;
}
}
任何人都可以帮我解决这个问题吗?我正在使用这个,这样他们就可以开始打字,而不是有一英里长的下拉列表......
I have implemented the Ajax Autocomplete feature in my application using a web service file that querys my database and it works great. One problem I am having is allowing the user to see the item's name, as that's what they are typing in the textbox, but when they select it, it saves the item's ID number instead of the actual name. I want it to behave much like a dropdown list, where I can specify what is seen and entered vs. what is actually saved in the database (in this case, the product ID instead of it's name.)
I have this text box in my view, along with the script:
<script type="text/javascript">
Sys.Application.add_init(function() {
$create(
AjaxControlToolkit.AutoCompleteBehavior, {
serviceMethod: 'ProductSearch',
servicePath: '/ProductService.asmx',
minimumPrefixLength: 1,
completionSetCount: 10
},
null,
null,
$get('ProductID'))
});
</script>
<p>
<label for="ProductID">Product:</label>
<%= Html.TextBox("ProductID", Model.Products)%>
<%= Html.ValidationMessage("ProductID", "*")%>
</p>
Here's what is in my asmx file:
public class ProductService : System.Web.Services.WebService
{
[WebMethod]
public string[] ProductSearch(string prefixText, int count)
{
MyDataContext db = new MyDataContext();
string[] products = (from product in db.Products
where product.ProductName.StartsWith(prefixText)
select product.ProductName).Take(count).ToArray();
return products;
}
}
Can anyone help me figure this out? I'm using this so they can just start typing instead of having a dropdown list that's a mile long...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自动完成控件会将 json 对象发布到“servicePath/serviceMethod”,因此首先将
servicePath
设置为您的控制器,并将serviceMethod
选项设置为您的操作名称。然后定义这个类:
因为这是自动完成控件发布到控制器的 json 对象。
然后定义一个 JsonModelBinder:
这个使用 Json.Net 作为反序列化器。
然后像这样定义您的操作:
注意我如何在 AutoCompleteRequest 参数上使用 JsonBinderAttribute 以及如何返回 Json 字符串数组。
编辑
我在这里写了一篇后续博客文章:http://devcarl.posterous.com/how-to-use-ajax-library-4-autocomplete-with-a
the autocomplete control will post a json object to "servicePath/serviceMethod" so first, set the
servicePath
to your controller and theserviceMethod
option to your action name.then define this class:
because thats the json object the autocomplete control posts to your controller.
then define a JsonModelBinder:
this one is using Json.Net as deserializer.
then define your action like this:
note how i use the JsonBinderAttribute on the AutoCompleteRequest parameter and how a Json array of strings is returned.
Edit
i did a follow up blog post here: http://devcarl.posterous.com/how-to-use-ajax-library-4-autocomplete-with-a
虽然这并不能直接回答您的问题,您是否考虑过使用 Jquery 自动完成控件?
根据我的经验,它似乎更灵活,您可以将其与现有服务链接起来,并对返回的数据有更多的控制。如果有帮助的话,这里是链接。
Although this doesn't answer your question directly have you thought about using the Jquery autocomplete control?
From my experience it seems more flexible and you could link it up with your existing service and have more control over the data that is returned as well. Here the link if it helps.