如何发送 jQuery $.get 以便模型绑定器可以将字符串绑定到参数?

发布于 2024-09-12 03:45:21 字数 1795 浏览 3 评论 0原文

我在使模型活页夹工作时遇到困难。我以为是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是偏爱你 2024-09-19 03:45:21

查看您的路线。你已经将控制器硬编码为“搜索”,如下所示:

routes.MapRoute(
    null,
    "search/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", searchExpression = "" }
);

如果你将其“软化”为:

routes.MapRoute(
    null,
    "{controller}/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", searchExpression = UrlParameter.Optional }
);

另外,从 $.get 切换到 $.ajax (帖子)可能会“有趣”。

只是另一个漫无目的的想法..

吉姆

looking at your route. you've got the controller hardcoded as 'search', as in:

routes.MapRoute(
    null,
    "search/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", searchExpression = "" }
);

what happens if you 'soften' this to:

routes.MapRoute(
    null,
    "{controller}/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", searchExpression = UrlParameter.Optional }
);

Also, might be 'interesting' to switch from $.get to $.ajax (post).

just another rambling thought..

jim

冷了相思 2024-09-19 03:45:21

也许我遗漏了一些东西,但我只会构建正常的获取网址:

$.get($(this).attr("href") + "/" + searchTerm, null, function (result) {

Perhaps I'm missing something but I would just build the normal get url:

$.get($(this).attr("href") + "/" + searchTerm, null, function (result) {
不弃不离 2024-09-19 03:45:21

您可以创建一个在使用 jQuery 搜索时使用的不同操作方法:

public PartialViewResult JQuerySearchForTabs(string q)
{
    return Tabs(q);
}


public PartialViewResult Tabs(string searchExpression)
{
    // This goes unchanged
}

然后将您的 jQuery 调用更改为

$.get('YourControllerName/JQuerySearchForTabs', { q: "schroders" }, function (result) {

    // do stuff

}, "html");

如果您不想对 url 进行硬编码而是从元素中获取它,请使用约定,例如将 < code>WithAjax 位于 url 末尾,因此您可以使用 $(this).attr('href') + 'WithAjax' 作为 url。

You could just create a different action method that you use when searching with jQuery:

public PartialViewResult JQuerySearchForTabs(string q)
{
    return Tabs(q);
}


public PartialViewResult Tabs(string searchExpression)
{
    // This goes unchanged
}

Then change your jQuery call to

$.get('YourControllerName/JQuerySearchForTabs', { q: "schroders" }, function (result) {

    // do stuff

}, "html");

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.

衣神在巴黎 2024-09-19 03:45:21

事实证明,我必须为不需要 searchExpression 的选项卡指定一个路由,因此我的路由配置现在如下所示:

routes.MapRoute(
    null,
    "search/Tabs",
    new { controller = "search", action = "Tabs" }
    );

routes.MapRoute(
    null,
    "search/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", 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:

routes.MapRoute(
    null,
    "search/Tabs",
    new { controller = "search", action = "Tabs" }
    );

routes.MapRoute(
    null,
    "search/{action}/{searchExpression}",
    new { controller = "search", action = "QuickSearch", searchExpression = "" }
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文