How to make the selected value in @Html.DropDownListFor as following code?
the following code populates the dropdown list for 1-31 under the date dropdown in my asp web.
@Html.DropDownListFor(
model => model.eDate,
Enumerable.Range(1, 31).Select(i => new SelectListItem
{ Value = i.ToString(), Text = i.ToString() }))
Now I am struggling to have a value in that dropdown preselected (e.g. i want the current date is an selected value). I a开发者_运维问答ssume today is 3 and then I have added the code to check if it is 3 and set the value of the Selected property accordingly... It does not work - no selected attribute in <option>
when I view the source in browser.
@Html.DropDownListFor(
model => model.eDate,
Enumerable.Range(1, 31).Select(i => new SelectListItem
{ Value = i.ToString(), Text = i.ToString(), Selected = i == 3 }))
Can expert enlighten me please?
Try this, it work for me
Controller:
public ActionResult Index()
{
var mymodel = new MyModel() {
eDate = DateTime.Now.Day
};
return View(mymodel);
}
View:
@Html.DropDownListFor(
model => model.eDate,
Enumerable.Range(1, 31).Select(i => new SelectListItem
{ Value = i.ToString(), Text = i.ToString() }))
精彩评论