【发布时间】:2014-12-04 15:06:10
【问题描述】:
我有以下代码: 控制器方法:
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 ,我想在其基础上设置类型,否则设置为红色。我已经尝试了很多,但对我没有任何帮助。任何帮助将不胜感激!
【问题讨论】:
-
你的模型是什么样的?
-
设置 AccountType 设置默认
-
我添加了模型@CodeCaster
-
请指导我如何? @MattBodily