Click here to Skip to main content
14,739,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListBoxFor where i need to retrieve all the selected values. The selected values are in a string array of the form :
string commaDelimited = "mihr20, emp1004, fbc567,mihr01";.
Now when i attempt to separate the individual items between the commas i get type-casting exception 'Cannot convert Char to int'. I need to extract the values to :

mihr20 emp1004 fbc567 mihr01

In the controller i have :

request.EmployeeNumber = new string[] { Convert.ToString(formcollection["EmployeeNumber"]) };
 string[] employees =request.EmployeeNumber.Split(new char[] { ',' });
if (ModelState.IsValid)
                {
                    foreach (string emp in employees)
                    {
					...save the selected employees
					}
					
					}



And the model as
[Required]
       [Display(Name = "Employee ")]
       public string[] EmployeeNumber { get; set; }
       public Employee Employee { get; set; }
       public String DisplayName { get; set; }
       public IEnumerable<SelectListItem> employees { get; set; }


What I have tried:

I tried to as below but in the database it saves a single value System.String[] instead of all the values

var employeees = request.EmployeeNumber.ToString();
 string[] employees =employeees.Split(new char[] { ',' });
Posted
Updated 26-Dec-19 2:32am

1 solution

This looks like the same issue as in your previous question at How do I resolve error 'cannot implicitly convert type 'string' to string[][^]. You need to understand the difference between a string, which is a single string of characters, and a string[], which is an array of string objects.
As to your problem here, you have not explained which line of code produces the error message above.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900