我有以下代码: 控制器方法:如何在MVC设定的默认值Html.DropDownListFor
public ActionResult Register(int? registrationTypeId)
{
IEnumerable<AccountType> accountTypes = new List<AccountType>
{
new AccountType
{
AccountTypeId = 1,
AccountTypeName = "Red"
},
new AccountType
{
AccountTypeId = 2,
AccountTypeName = "Blue"
}
};
// I want to select account type on registrationTypeId
ViewBag.AccountTypes = accountTypes;
return View();
}
查看
<div class="col-md-10">
@Html.DropDownListFor(n => n.AccountType,
new SelectList(ViewBag.AccountTypes, "AccountTypeId", "AccountTypeName"), new { @class = "form-control" })
</div>
型号
public class RegisterViewModel
{
[Required]
[Display(Name = "Account Type")]
public int AccountType { get; set;
}
正如你可以看到registrationTypeId在控制器中,我想设置它的基础上的类型,如果它不是null,否则设置为红色。我尝试了很多,但没有为我工作。任何帮助将不胜感激 !
你的模型是什么样的? – CodeCaster 2014-12-04 15:08:40
[我怎样才能让这个ASP.NET MVC SelectList工作?](http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-工作) – CodeCaster 2014-12-04 15:09:13
设置AccountType设置默认 – 2014-12-04 15:09:33