я следующий код: метод контроллера:Как установить значение по умолчанию для Html.DropDownListFor в MVC
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
Возможный дубликат [Как я могу заставить этот ASP.NET MVC SelectList работать?] (Http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist- для работы) – CodeCaster
set AccountType для установки по умолчанию –