2

I'm using ViewData to pass a List<string> to my View:

// Controller:
ViewData["myList"] = new SelectList(new List<string>(new[] "AAA", "BBB", "CCC" }));

I use this List to populate a ListBox:

// View:
@Html.ListBox("myList")

On Post I retrieve the selected items, like so:

// Controller:
string myList = form["myList"]

So far so good, but the selected items are all cleared on Post.
How do I make selected items persist across requests?

tereško
  • 58,060
  • 25
  • 98
  • 150
Administrateur
  • 891
  • 4
  • 13
  • 29
  • with mvc if you pass values through your model to the view and use for helpers on the view (textboxfor, textareafor, etc) the selected items will be passed back to the controller through the model – Matt Bodily Oct 03 '13 at 19:42
  • @MattBodily: Yes. I get the selected items in my Post Action... but how do I tell my View to select those items back? – Administrateur Oct 03 '13 at 19:47
  • here is another answer given by Darin that might help http://stackoverflow.com/questions/6290108/mvc3-razor-listbox-pre-select-not-working – Matt Bodily Oct 03 '13 at 19:54
  • @MattBodily: Thank you. I already tried something like that but it didn't work: `ViewData["myList"] = new SelectList(new List(new[] "AAA", "BBB", "CCC" }), "AAA")` – Administrateur Oct 03 '13 at 20:06

4 Answers4

1

Since MVC does not have any mechanism like viewstate or controlstate the data can not be persisted across the requests automatically. So with every request you will have to create the page as you want it to be delivered. On post when you get the selected item, you will have to send the value to the view to be selected for the next load.

Here is a link where you can get a working code.

Community
  • 1
  • 1
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • Yes. This is exactly what I need. How do I accomplish this using my example above? – Administrateur Oct 03 '13 at 19:48
  • I couldn't get my problem resolved by following the example post you mentioned. I tried something like this `@Html.ListBox("myList", (SelectList)ViewData["mySelection"])`. I selected "AAA" and when the page Posted "AAA" was the only item in the ListBox ("BBB", and "CCC" were not there any more). – Administrateur Oct 03 '13 at 20:32
  • That is correct, in MVC no data persists on post back. For the next load now you will have to bind the list and set the selection. Hope it helps. Let me know if you have further queries. – Shashank Chaturvedi Oct 03 '13 at 20:46
  • Right. I need help with the **"set the selection"** part. Would you give an example? – Administrateur Oct 03 '13 at 21:11
  • This link would help you http://www.obout.com/mvc-listbox/selection/aspnet_selection_multi_set.aspx – Shashank Chaturvedi Oct 03 '13 at 21:26
1

As mentioned already, MVC has no ViewState mechanism, so the values that you want rendered in the view have to be instantiated with each request.

Here is a fairly crude example, but should outline what you need to do:

public ActionResult Index()
{
    ViewData["myList"] = GetSelectList();
    return View();
}

[HttpPost]
public ActionResult Index(FormCollection form)
{
    ViewData["myList"] = GetSelectList(form["myList"]);
    return View();
}

private MultiSelectList GetSelectList(string selected = "")
{
    var selectedValues = selected.Split(',');
    return new MultiSelectList(new List<string>(new[] { "AAA", "BBB", "CCC" }), selectedValues);
}

View markup

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    @Html.ListBox("myList")
    <input type="submit" value="Submit" />
}
Brent Mannering
  • 2,316
  • 20
  • 19
  • How would the View look like? This is what I have `@Html.ListBox("myList")`. So when I run your code, instead of ListBox displaying **"AAA", "BBB", "CCC"**, etc, it displays **"System.Web.Mvc.SelectListItem", "System.Web.Mvc.SelectListItem", "System.Web.Mvc.SelectListItem"** – Administrateur Oct 03 '13 at 21:58
  • @Administrateur sorry hadn't tested the code, had assumed there would be enough there to work on. None the less I've updated GetSelectList method and added the view markup, and tested it, let me know how you get on. – Brent Mannering Oct 03 '13 at 22:18
0

If you are using listbox which means you can select several you have to use MultiselectList

ViewData["myList"] = new MultiSelectList(new List<string>(new[] "AAA", "BBB", "CCC" }));
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

You could add the data into a session object.

Session["myList"] = your List<string>

When you need to pull it back out of the session, use...

List<string> myList = (List<string>)Session["myList"];

using your code,

var selectList = new SelectList(new List<string> {"AAA", "BBB", "CCC"});
Session["myList"] = selectList; 

Then, if you want to assign it in your controller to ViewData...

ViewData["myList"] = (SelectList)Session["myList"]; //may not need the cast.

@Html.ListBox("myList")
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82