kris

kris

  • NA
  • 26
  • 19.5k

List<string> passed in ViewBag gets with “System.Collections

Oct 3 2014 4:05 AM
In MVC 4
 I have a link to another controller and I want to pass a list of strings.

FirstController.cs

I have list of records in table with checkbox in each row, List of string values are selected by checking the checkbox.

public ActionResult Select(bool isChecked, String id)
 { 
var selectList = (List<String>)HttpContext.Session["SelectList"] ?? new List<String>(); 
if (isChecked && !selectList.Contains(id))
 {               
  selectList.Add(id); } else if (!isChecked && selectList.Contains(id)) 
{                 
selectList.RemoveAll(s => s == id);
 } 
ViewBag.selectList = selectList; 
return Content("OK"); }
The list of string values i am passing to SecondController.cs using 'ViewBag'
public ActionResult AddSitesToUser(string returnUrl,string selectList, int userId = 0) 
{ 
ViewBag.selectList = selectList;
 }
View:
(Html.BeginForm("AddSitesToUser", "User", new { returnUrl = ViewBag.returnUrl,selectList=((List<string>)ViewBag.selectList )})

In my FirstController.cs the list of ViewBag.selectList = selectList; created properly, but if i pass those list of strings to SecondController.cs instead of returning list of strings it gives me '"System.Collections.Generic.List1[System.String]"

Any idea on what I'm doing wrong?

Thanks

 

Answers (1)