In MVC 4
I have a link to another controller and I want to pass a list of strings.The list of string values i am passing to SecondController.cs using 'ViewBag'View:
FirstController.cs
I have list of records in table with checkbox in each row, List of string values are selected by checking the checkbox.
Code
public ActionResult Select(bool isChecked, String id)Code
{Code
var selectList = (ListString>)HttpContext.Session["SelectList"] ?? new ListString>();Code
if (isChecked && !selectList.Contains(id))Code
{Code
selectList.Add(id); } else if (!isChecked && selectList.Contains(id))Code
{Code
selectList.RemoveAll(s => s == id);Code
}Code
ViewBag.selectList = selectList;Code
return Content("OK"); }Code
public ActionResult AddSitesToUser(string returnUrl,string selectList, int userId = 0)Code
{Code
ViewBag.selectList = selectList;Code
}Code
(Html.BeginForm("AddSitesToUser", "User", new { returnUrl = ViewBag.returnUrl,selectList=((List)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

Pradeep ShetRank #595 · 4K reputationPosted Oct 3, 2014, 3:27 AM EDT
Definitely it will give you this result because you are passing list as a part of html attributes, which is wrong. Why you are passing it in BeginForm?
You can access ViewBag directly in the View. It you want to access the data after postback then you can use TempData(). This will help you to post the same List