如何发送的jQuery $不用彷徨所以模型绑定可以绑定一个字符串参数? [英] How to send jQuery $.get so the Model Binder can bind a string to a parameter?

查看:113
本文介绍了如何发送的jQuery $不用彷徨所以模型绑定可以绑定一个字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难得到模型绑定工作。我认为这是jQuery的,所以我问这个问题,但经过进一步调查,我可以看到的jQuery确实发送参数到服务器。这是我要问一个新的问题的原因 - 这不再是一个jQuery的问题,因为我原本以为

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.

背景:

我在做什么是发送一个GET请求到我的行动方法如下:

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");

这将创建下列网址:

的http://本地主机:65091 /搜索/标签searchEx pression =施罗德

我认为这会工作,并填充操作方法:

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 = "" }
    );

我们可以看到,searchEx pression有望作为URL参数,而不是一个查询字符串参数。我不认为这会是一个问题,但如果我超载标签如下:

public PartialViewResult Tabs(string searchExpression, string query)
{
    return PartialView(new SearchViewModel
    {
        PagedFunds = _fundService.GetFunds(searchExpression)
    });
}

和变化

{ "searchExpression": "schroders" }

{ "query": "schroders" }

在操作方法的查询参数填充。

the query parameter in the Action Method is populated.

问:

所以我的问题是需要改变什么得到填充searchEx pression?我是否需要修改的jQuery的,因此附加宝源的URL,因此它就像

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.

推荐答案

原来我必须指定用于该并不需要标签的路线 searchEx pression ,所以我的路由配置现在看起来像:

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 = "" }
    );

这篇关于如何发送的jQuery $不用彷徨所以模型绑定可以绑定一个字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆