如何发送 jQuery $.get 以便模型绑定器可以将字符串绑定到参数?
我在使模型活页夹工作时遇到困难。我以为是 jQuery,所以我问了 这个问题,但是经过进一步调查,我可以看到 jQuery 确实正在将参数发送到服务器。这就是我提出一个新问题的原因 - 这不再是一个 jQuery 问题,正如我最初所想的那样。
背景:
我正在做的是将 GET 请求发送到我的操作方法,如下所示:
$.get($(this).attr("href"), { "searchExpression": "schroders" }, function (result) {
// do stuff
}, "html");
这将创建以下 URL:
http://localhost:65091/search/Tabs?searchExpression=schroders
我认为这会起作用,并填充了操作方法:
public PartialViewResult Tabs(string searchExpression)
{
return PartialView(new SearchViewModel
{
PagedFunds = _fundService.GetFunds(searchExpression)
});
}
但定义了此方法的路由as:
routes.MapRoute(
null,
"search/{action}/{searchExpression}",
new { controller = "search", action = "QuickSearch", searchExpression = "" }
);
正如我们所看到的,searchExpression 应该作为 URL 参数,而不是查询字符串参数。我不认为这会成为问题,但如果我按如下方式重载 Tabs
:
public PartialViewResult Tabs(string searchExpression, string query)
{
return PartialView(new SearchViewModel
{
PagedFunds = _fundService.GetFunds(searchExpression)
});
}
并更改
{ "searchExpression": "schroders" }
为
{ "query": "schroders" }
填充操作方法中的 query
参数。
问题:
所以我的问题是需要更改什么才能填充 searchExpression?我是否需要修改 jQuery,以便将“schroders”附加到 URL,所以
/search/Tabs/schroders
理想情况下,我可以两全其美,用户可以使用搜索词键入 URL,而且我也可以使用 $.获得一种可以将搜索词作为参数传递给 $.get 函数的方式。
I'm having difficulty getting the Model Binder to work. I thought it was the jQuery, so I asked this question, but after further investigation, I can see that the jQuery is indeed sending the parameter to the server. That's the reason I'm asking a new question - this is no longer a jQuery issue, as I originally thought.
Background:
What I'm doing is sending a GET request to my Action Method as follows:
$.get($(this).attr("href"), { "searchExpression": "schroders" }, function (result) {
// do stuff
}, "html");
this creates the following URL:
http://localhost:65091/search/Tabs?searchExpression=schroders
I thought this would have worked, and populated the Action Method:
public PartialViewResult Tabs(string searchExpression)
{
return PartialView(new SearchViewModel
{
PagedFunds = _fundService.GetFunds(searchExpression)
});
}
but the route to this method is defined as:
routes.MapRoute(
null,
"search/{action}/{searchExpression}",
new { controller = "search", action = "QuickSearch", searchExpression = "" }
);
As we can see, searchExpression is expected as a URL parameter, not a query string parameter. I didn't think this would be an issue, but if I overload Tabs
as follows:
public PartialViewResult Tabs(string searchExpression, string query)
{
return PartialView(new SearchViewModel
{
PagedFunds = _fundService.GetFunds(searchExpression)
});
}
and change
{ "searchExpression": "schroders" }
to
{ "query": "schroders" }
the query
parameter in the Action Method is populated.
Question:
So my question is what needs to change to get the searchExpression populated? Do I need to modify the jQuery so it appends "schroders" to the URL, so it's like
/search/Tabs/schroders
Ideally I could have the best of both worlds, where the user could type the URL with the search term, and I could also use the $.get in a way that I could pass the search term as a parameter to the $.get function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看您的路线。你已经将控制器硬编码为“搜索”,如下所示:
如果你将其“软化”为:
另外,从 $.get 切换到 $.ajax (帖子)可能会“有趣”。
只是另一个漫无目的的想法..
吉姆
looking at your route. you've got the controller hardcoded as 'search', as in:
what happens if you 'soften' this to:
Also, might be 'interesting' to switch from $.get to $.ajax (post).
just another rambling thought..
jim
也许我遗漏了一些东西,但我只会构建正常的获取网址:
Perhaps I'm missing something but I would just build the normal get url:
您可以创建一个在使用 jQuery 搜索时使用的不同操作方法:
然后将您的 jQuery 调用更改为
如果您不想对 url 进行硬编码而是从元素中获取它,请使用约定,例如将 < code>WithAjax 位于 url 末尾,因此您可以使用
$(this).attr('href') + 'WithAjax'
作为 url。You could just create a different action method that you use when searching with jQuery:
Then change your jQuery call to
If you don't want to hard-code the url but get it from the element, use a convention of for example tucking on
WithAjax
at the end of the url, so you can say$(this).attr('href') + 'WithAjax'
for the url.事实证明,我必须为不需要
searchExpression
的选项卡指定一个路由,因此我的路由配置现在如下所示:It turns out I had to specify a route to use for the Tabs that didn't need
searchExpression
, so my routing configuration now looks like: