从ajax调用(通过令牌输入)传递一组int到控制器

时间:2021-10-28 19:34:33

I've been trying to pass an array of int's created by the following function:

我一直在尝试传递由以下函数创建的int数组:

function getCurrentSwimmerList() {
    var swimmerList = [];
    $("#swimmerTable >  tbody > tr").each(function () {
        swimmerList.push( parseInt($(this).data('swimmerid')) );
    });
    return swimmerList;
}

Which I use in token-input to eliminate ceartin suggestions from the search box, so I've set up token-input up like so:

我在令牌输入中使用它来从搜索框中消除ceartin建议,所以我设置了令牌输入,如下所示:

$("#swimmerTokenInput").tokenInput("Admin/retrieveTokensForQuery", {
            urlParams: { "IDsAlreadyAdded": getCurrentSwimmerList },

I modified the token-input file to allow you to pass additional paramters in a request by setting urlParams, the addition I made to the code was (in the appropriate section):

我修改了令牌输入文件,允许你通过设置urlParams在请求中传递额外的参数,我对代码的添加是(在适当的部分):

//add params passed in as urlParams
if (settings.urlParams != null) {
    for (var key in settings.urlParams) {
        if (settings.urlParams.hasOwnProperty(key)) {
            ajax_params.data[key] = settings.urlParams[key];
        }
    }
}

I tested and I successfully get these values in the query string (where Ol was typed into the search box):

我测试了,我成功地在查询字符串中获取了这些值(在搜索框中输入了Ol):

IDsAlreadyAdded=5%2C6&q=Ol

Which chrome recognizes and parses correctly:

哪个chrome可以正确识别和解析:

IDsAlreadyAdded:5,6
q:Ol

The signature of the method I'm calling is as follows:

我正在调用的方法的签名如下:

public JsonResult retrieveTokensForQuery(string q, int[] IDsAlreadyAdded)

Each time q successfully get's the appropriate value, however IDsAlreadyAdded always gets a null value. I've looked at various answers on SO (trying traditional=true, or IdsAlreadyAdded[] = ..., having List<int> or IEnumerable<int>) to try fix the problem, but I couldn't get it to work.

每次q成功获得适当的值,但是IDAlreadyAdded总是得到一个空值。我已经看了SO上的各种答案(尝试传统= true,或IdsAlreadyAdded [] = ...,有List 或IEnumerable )来尝试修复问题,但我无法让它工作。

Any help would be greatly appreciated.

任何帮助将不胜感激。

1 个解决方案

#1


1  

You could always create a custom model binder that maps the string value to an int[] like this:

您总是可以创建一个自定义模型绑定器,将字符串值映射到int [],如下所示:

public class IntArrayModelBinder : System.Web.Mvc.DefaultModelBinder
{
    public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (!string.IsNullOrEmpty(valueProviderResult.AttemptedValue))
        {
            var items = valueProviderResult.AttemptedValue.Split(',');
            var result = new int[items.Length];
            for (var counter = 0; counter < items.Length; counter++)
            {
                result[counter] = int.Parse(items[counter]);
            }
            return result;
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}

and then just register the binder when you initialise your routing:

然后在初始化路由时注册绑定器:

ModelBinders.Binders.Add(typeof(int[]), new IntArrayModelBinder());

#1


1  

You could always create a custom model binder that maps the string value to an int[] like this:

您总是可以创建一个自定义模型绑定器,将字符串值映射到int [],如下所示:

public class IntArrayModelBinder : System.Web.Mvc.DefaultModelBinder
{
    public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (!string.IsNullOrEmpty(valueProviderResult.AttemptedValue))
        {
            var items = valueProviderResult.AttemptedValue.Split(',');
            var result = new int[items.Length];
            for (var counter = 0; counter < items.Length; counter++)
            {
                result[counter] = int.Parse(items[counter]);
            }
            return result;
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}

and then just register the binder when you initialise your routing:

然后在初始化路由时注册绑定器:

ModelBinders.Binders.Add(typeof(int[]), new IntArrayModelBinder());