简体   繁体   English

MVC如何将数组(Guid CustomerID)和单个值传递给控制器

[英]MVC How To Pass Array(Guid CustomerIDs) And a Single Value To Controller

I am using JavaScript and ajax to pass an array and a single value to the controller, When controller method being call the single value is passed in but not the (array of customerID) and I wonder why, here is the code! 我正在使用JavaScript和Ajax将数组和单个值传递给控制器​​,当调用被调用的控制器方法时,传递的是单个值而不是(customerID的数组),我想知道为什么,这是代码!

Here is my JavaScript and ajax call: 这是我的JavaScript和Ajax调用:

var arrayOfCustomerID = ['11111111 - 1111 - 1111 - 1111 - 111111111112', '4b732df8-3bbc-4eab-9bf1-da98cc46d527' ,
'3e74adeb-31a8-4d6f-a66b-a34500c3acc0',' 14897bf4-f5d0-4548-a74d-a34f00ae9cad'];

var version = '3.0';

$.ajax({
         url: '/Updates/UpdateCustomers',
         type: "POST",
         datatype: "json",
         traditional: true,
         data: {
           CustomerIDs: JSON.stringify(arrayOfCustomerID),
           Version: version
               },
         success: function (data) {
         alert("success");
         }
       });

Here is my class: 这是我的课:

public class SelectedCustomersWithVersion 
    {
        public List<Guid> CustomerIDs { get; set; }

        public string version { get; set;  }
    }

Finally here is my controller method: 最后是我的控制器方法:

[HttpPost]
public ActionResult UpdateCustomers(SelectedCustomersWithVersion model)
{
 IList a = model.CustomerIDs.ToList();
 var b = model.version;
 return view();
}

when I put a break point on my controller model I got model.CustomerIds: Count = 0, model.Version: "3.0" 当我在控制器模型上设置一个断点时,我得到了model.CustomerIds:Count = 0,model.Version:“ 3.0”

I don't know why I am getting Customerids= 0 count, is there something wrong with the code? 我不知道为什么要获得Customerids = 0计数,代码有问题吗? Thanks for help ! 感谢帮助 !

This is due to the way you are passing the parameters to the action method. 这是由于您将参数传递给action方法的方式。 In your current approach you are sending them as one parameter which is received as a comma separated string which the binder doesn't convert into a List<Guid> . 在当前方法中,您将它们作为一个参数发送,并以逗号分隔的字符串形式接收,而活页夹不会将其转换为List<Guid>

The following Fiddler capture shows how you are sending your parameters: 以下Fiddler捕获显示了如何发送参数:

在此处输入图片说明

In order for the model binder to correctly bind a List<T> the parameters need to be sent separately either with the same name or with an index assigned. 为了使模型绑定器正确绑定List<T> ,需要使用相同的名称或分配的索引分别发送参数。

For example, the following 2 approaches will both work: 例如,以下两种方法都将起作用:

?customerIDs=1111...&customerIDs=4b73...&customerIDs=3e...

?customerIDs[0]=1111...&customerIDs[1]=4b73...&customerIDs[2]=3e...

In the second example note that the indexes have to be zero based and without gaps. 在第二个示例中,索引必须从零开始且无间隙。

In order to get this to work you can change your jQuery code to: 为了使它起作用,您可以将jQuery代码更改为:

arrayOfCustomerID = [];
arrayOfCustomerID[0] = '11111111 - 1111 - 1111 - 1111 - 111111111112';
arrayOfCustomerID[1] = '4b732df8-3bbc-4eab-9bf1-da98cc46d527';
arrayOfCustomerID[2] = '3e74adeb-31a8-4d6f-a66b-a34500c3acc0';
arrayOfCustomerID[3] = ' 14897bf4-f5d0-4548-a74d-a34f00ae9cad';

var version = '3.0';

$.ajax({
    url: '/api/values',
    type: "POST",
    datatype: "json",
    traditional: true,
    data: {
        CustomerIDs: arrayOfCustomerID,
        Version: version
    },
    success: function (data) {
        alert("success");
    }
});

This will send the parameters like this: 这将发送如下参数:

在此处输入图片说明

Which will then successfully get bound to your model: 然后将成功绑定到您的模型:

在此处输入图片说明

One final note, I had to remove the spaces from your first Guid in order to get it to bind. 最后一点,我必须从您的第一个Guid中删除空格以使其绑定。

I think MVC is not able to automatically bind an array of strings (client side) to an array of GUIDs (server side). 我认为MVC无法自动将字符串数组(客户端)绑定到GUID数组(服务器端)。

Try changing your view model ( SelectedCustomersWithVersion ) to use strings instead. 尝试将视图模型( SelectedCustomersWithVersion )更改为使用字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM