ASP.NET MVC (razor) Listbox multiselect values to string?

You need to set Selected property of SelectItemList class to true for the options which you want to be pre-selected.

Up vote 0 down vote favorite share g+ share fb share tw.

I have two multi-select list boxes; one with months and one with years such as the following: @Html. ListBoxFor(m => m. IncludeGuestsArrivedInTimeframeMonths, Model.

TimeframeMonths) @Html. ListBoxFor(m => m. IncludeGuestsArrivedInTimeframeYears, Model.

TimeframeYears) I'm saving the both values to the database as a comma separated list (string/nvarchar). The values are being like this: Months: 1,3,7; Years: 2002,2005 For some reason when I pull the values back out to the form, the months are pre-selecting in the listbox fine, but the years are not. Any ideas?

EDIT - Additional code samples: Controller - Manage public ActionResult Manage(Guid id) { var list = _listService. GetList(id); var model = LoadModelFromObject(list); model. TimeframeMonths = GenerateMonthDropdown(); model.

TimeframeYears = GenerateYearDropdown(model. IncludeGuestsArrivedInTimeframeYears. Split(',')); model.

DaysOfWeek = GenerateDaysOfWeekDropdown(); return View(model); } Controller - 'Helper' private IList GenerateYearDropdown(string selected) { var list = new List(); var startYear = DateTime.Now. Year - 10; for (int idx = startYear; idx m. IncludeGuestsArrivedInTimeframe) @Html.

LabelFor(m => m. IncludeGuestsArrivedInTimeframe) @Html. ListBoxFor(m => m.

IncludeGuestsArrivedInTimeframeMonths, Model. TimeframeMonths) @*@Html. ListBoxFor(m => m.

IncludeGuestsArrivedInTimeframeYears, Model. TimeframeYears)*@ @Html. ListBox("IncludeGuestsArrivedInTimeframeYears", Model.

TimeframeYears) asp. Net-mvc razor link|improve this question edited Jan 4 at 18:21 asked Jan 4 at 15:13mmillican196 62% accept rate.

What does Model. TimeframeYears return? I mean the values.

It must return type of IEnumerable but I cannot see what values it has. – tugberk Jan 4 at 15:28 @tugberk The Model. TimeframeYears has the IList and the Value and Text of each SelectListItem are the year (2002) – mmillican Jan 4 at 15:46 Could you provide sample values illustrating the problem of the IncludeGuestsArrivedInTimeframeYears and TimeframeYears properties?

– Darin Dimitrov Jan 4 at 17:45 See my DDL tutorials asp.net/mvc/tutorials/javascript/… and blogs.msdn.com/b/rickandy/archive/2012/01/09/… – RickAndMSFT Jan 4 at 19:54.

You need to set Selected property of SelectItemList class to true for the options which you want to be pre-selected. Example: @{ var foo = new List { new SelectListItem { Text = "Foo 1", Value = "1", Selected = true }, new SelectListItem { Text = "Foo 2", Value = "2" }, new SelectListItem { Text = "Foo 3", Value = "3", Selected = true } }; } @Html. ListBox("foo", foo) In this case, Foo 1 and Foo 3 should appear as pre-selected.

The above code generates the below html markup: Foo 1 Foo 2 Foo 3 Edit: First of all, replace this code: list. Add(new SelectListItem { Value = idx.ToString(), Text = idx.ToString(), Selected = selected! = null && selected.

Contains(idx.ToString()) }); with this code: list. Add(new SelectListItem { Value = idx.ToString(), Text = idx.ToString(), Selected = true }); Then run the app. If all the options are pre-selected, then I bet that this selected!

= null && selected. Contains(idx.ToString()) does not satisfy the condition.

Okay, I changed it up a little bit. I have a method called GenerateYearDropdown which I modified to take an array of strings, which I pass in. The array is being passed in correctly, and setting the selected value of each list item, but it's not displaying those selections on the form.

– mmillican Jan 4 at 16:55 @mmillican did you try out the above code? It works on my machine and should work on yours, too. The problem is with your code, post it as well.

– tugberk Jan 4 at 17:41 I updated my original post with additional code. Thanks! – mmillican Jan 4 at 18:22 @mmillican updated the answer.

– tugberk Jan 4 at 18:34 1 I updated it and set Selected = true for all the entries and it's still not selecting them. Any other ideas? Sorry for the hassle!

– mmillican Jan 4 at 18:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions