Reputation: 4266
today I'm having trouble with Default Value for a SelectList in ASP.NET.
here is how I do after trying lots of things I found in the Internet:
@{
IEnumerable<SelectListItem> secteurSelectList = from x in Model.secteurList select new SelectListItem {
Selected = (x.Id == this.GetSessionSecteurId()),
Text = x.Secteur,
Value = x.Id.ToString()
};
SelectList selectList = new SelectList(secteurSelectList, "Value", "Text", secteurSelectList.Where(x => x.Selected==true).FirstOrDefault().Value); }
@Html.DropDownListFor(m => m.secteur, selectList)
Here is the description of My entities: In the ViewModel I use
public List<AuditSecteur> secteurList { get; set; }
Here is the AuditSecteur's object:
public class AuditSecteur
{
public int Id { get; set; }
public string Secteur { get; set; }
}
This is the result:
<select data-val="true" data-val-number="The field Secteur de l'audit must be a number." data-val-required="The Secteur de l'audit field is required." id="secteur" name="secteur">
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
SecteurSelectedList has one item at true for the selected: SelectedList too:
Thanks to help me
Upvotes: 0
Views: 584
Reputation:
The whole purpose of using the strongly typed helpers is to bind your model properties so in the case of
@Html.DropDownListFor(m => m.secteur, selectList)
if the value of secteur
does not match the value of one of your options, then it wont be selected (actually because it cant find a match, the first option will be selected because something needs to be selected).
But there are numerous other issues with your code. First you creating IEnumerable<SelectListItem>
(setting the Selected
property is pointless since its ignored by the helper), then you create another IEnumerable<SelectListItem>
(the SelectList
) based on the first one in the next line (whats the point?). The value of the options generated are based on the ID
property of AuditSecteur
but then you bind to to the Secteur
property so it would never post back a correct value anyway. Its not really clear how your using this (you have not included you model, controllers or view) but the correct approach would be something like
View model
public class SecteurVM
{
[Display(Name = "Secteur")]
[Required]
public int? SelectedSecteur { get; set; }
public SelectList SecteurList { get; set; }
}
Controller
public ActionResult Create()
{
List<AuditSecteur> secteurList = // get AuditSecteur items from the database
SecteurVM model = new SecteurVM();
model.SecteurList = new SelectList(secteurList, "ID", "Secteur");
model.SelectedSecteur = // set the default value you want selected here
return View(model)
}
[HttpPost]
public ActionResult Create(SecteurVM model)
{
// model.SelectedSecteur contains the ID of the selected item
}
View
@model SecteurVM
....
@Html.LabelFor(m => m.SelectedSecteur)
@Html.DropDownListFor(m => m.SelectedSecteur, Model.SecteurList, "--Please select--")
@Html.ValidationMessageFor(m => m.SelectedSecteur)
If the value of SelectedSecteur
matches one of the ID
properties of the items in the list, then that is the option that will be selected when you display the view.
Upvotes: 1
Reputation: 1086
you're binding DropDown to secteur
property of the Model while Value propety of the dropdown is Id
so dropdown won't find the corresponding value while making pre-selection.
here even if you have defined pre-selected
item it will reset
while rendering, so i would suggest bind you dropdown to Id
instead of secteur
.
@Html.DropDownListFor(m => m.Id, selectList)
Upvotes: 0