如何在MVC中为下拉列表设置默认值? [英] How to set a default value for dropdownlist in MVC?

查看:77
本文介绍了如何在MVC中为下拉列表设置默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为下拉列表设置默认值.

I am trying to set a default value for a dropdownlist.

控制器:

ViewData [年龄"] =新的SelectList(db.GetGateList(),"myAge");

ViewData["Ages"] = new SelectList(db.GetGateList(), "myAge");

查看:

< div> @ Html.DropDownListFor(model => model.Age,ViewData ["Ages"] as IEnumerable< SelectListItem> ;, new {style =" width:100px;"}})</div>

<div>@Html.DropDownListFor(model => model.Age, ViewData["Ages"] as IEnumerable<SelectListItem>, new { style = "width: 100px;"})</div>

现在,我要尝试的是将此下拉列表的默认值设置为选定的输入(例如30).

Now, what I am trying to do is to set the default value of this dropdownlist to selected input (say 30).

我尝试了许多不同的选择,但是找不到解决方案.

I tried many different options but couldn't find a solution.

任何建议都会得到赞赏.

Any suggestions would be appreciataed.

Thx

王牌

推荐答案

您好,OP:

假设您的默认选择为1,则可以执行以下操作:

Suppose your default choice is 1, and you can do this:

列表< SelectListItem> items =  ViewData [" Ages] =物品;

List<SelectListItem> items = db.GetGateList();
items[0].Selected=true;
ViewData["Ages"] = items;


这篇关于如何在MVC中为下拉列表设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆