admin管理员组

文章数量:1289581

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!

Here is my JavaScript and ajax call:

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"

I don't know why I am getting Customerids= 0 count, is there something wrong with the code? Thanks for help !

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!

Here is my JavaScript and ajax call:

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"

I don't know why I am getting Customerids= 0 count, is there something wrong with the code? Thanks for help !

Share Improve this question edited Aug 11, 2014 at 11:07 Sameul.T asked Aug 11, 2014 at 10:54 Sameul.TSameul.T 3091 gold badge3 silver badges17 bronze badges 4
  • Try without stringify - CustomerIDs: arrayOfCustomerID,` – user3559349 Commented Aug 11, 2014 at 11:12
  • still the same, got CustomerIDs = 0 count – Sameul.T Commented Aug 11, 2014 at 11:19
  • The first item in your array does not look like a GUID – user3559349 Commented Aug 11, 2014 at 11:20
  • MVC does pick up as guid and yeah it a test ID, I inputted manually. – Sameul.T Commented Aug 11, 2014 at 11:46
Add a ment  | 

2 Answers 2

Reset to default 8

This is due to the way you are passing the parameters to the action method. In your current approach you are sending them as one parameter which is received as a ma separated string which the binder doesn't convert into a List<Guid>.

The following Fiddler capture shows how you are sending your parameters:

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.

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:

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.

I think MVC is not able to automatically bind an array of strings (client side) to an array of GUIDs (server side).

Try changing your view model (SelectedCustomersWithVersion) to use strings instead.

本文标签: javascriptMVC How To Pass Array(Guid CustomerIDs) And a Single Value To ControllerStack Overflow