使用 MVC3/Razor 处理对象和路由?
我有一个地理集合,其中包含以下项目: [州名] [城市]、[州] [国家/地区]
一个文本框可供用户开始输入,并且一个 jQuery 自动完成框会填充并显示可能的选项。
post 请求的 URL 结构将取决于从上面的集合中选择的内容,即 www.mysite.com/allstates/someterms(如果选择了国家/地区) www.mysite.com/city-state/someterms(如果选择城市、州) www.mysite.com/[州名称]/someterms(如果选择了州)
这些已在我的路线中定义。
我最初打算在控制器上添加一些逻辑来确定适当的 URL 结构,但我想简单地将其添加为地理表中的附加字段,因此它将成为地理集合的属性。
这是我的 jQuery 函数,用于在文本框中按键时触发时显示集合详细信息:
$(function () {
$("#txtGeoLocation").autocomplete(txtGeoLocation, {
source: function (request, response) {
$.ajax({
url: "/home/FindLocations", type: "POST",
dataType: "json",
selectFirst: true,
autoFill: true,
mustMatch: true,
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
}))
}
})
},
select: function (event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
: "Nothing selected, input was " + this.value);
document.getElementById("hidLocation").value = ui.item.id;
}
});
});
我想要的是基于对象参数构建 URL(似乎是最简单的)。我似乎只能读取“选定”的参数,而不能读取按钮单击的参数。
我怎样才能做到这一点?
谢谢。
I have a geo collection that contains items like:
[state name]
[city], [state]
[country]
A text box is available for a user to begin typing, and a jQuery autocomplete box fills displays possible options.
The URL structure of the post request will depend on which was selected from the collection above, ie
www.mysite.com/allstates/someterms (if a country is selected)
www.mysite.com/city-state/someterms (if a city, state is selected)
www.mysite.com/[state name]/someterms (if a state is selected)
These are already defined in my routes.
I was initially going to add some logic on the controller to determine the appropriate URL structure, but I was thinking to simply add that as an additional field in the geo table, so it would be a property of the geo collection.
Here is my jQuery function to display the collection details when, fired on keypress in the textbox:
$(function () {
$("#txtGeoLocation").autocomplete(txtGeoLocation, {
source: function (request, response) {
$.ajax({
url: "/home/FindLocations", type: "POST",
dataType: "json",
selectFirst: true,
autoFill: true,
mustMatch: true,
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
}))
}
})
},
select: function (event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
: "Nothing selected, input was " + this.value);
document.getElementById("hidLocation").value = ui.item.id;
}
});
});
What I would like is to have structure the URL based on an object parameter (seems the simplest). I can only seem to read the parameters on "selected", and not on button click.
How can I accomplish this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决这个问题,我从 Javascript 中删除了
select:
部分,并在发送到控制器的 MVC 路由中添加了选定的对象参数。To resolve this, I removed the
select:
portion from the Javascript, and added the selected object parameters in the MVC route sent to my controller.