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[] { ',' });