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)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 ShetPosted Oct 3, 2014, 7:27 AM
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