[英]MVC 4/5 Add static dropdown to register with validation
On the register page I have added a dropdown for vdab student: Yes / no But the validation described in the viewmodel is not working (I guess this is because I used ViewBag and not model?) 在注册页面上,我为vdab学生添加了一个下拉列表:是/否但是viewmodel中描述的验证无效 (我想这是因为我使用ViewBag而不是模型吗?)
Can someone please help me to create the dropdown so I Can use this way: 有人可以帮我创建下拉菜单,这样我可以使用以下方式:
@Html.DropDownFor(x => x.Something, Model.Something, "Select a component type" )
This is what I have: 这就是我所拥有的:
IdentityModel 身份模型
public class ApplicationUser : IdentityUser
{
public virtual Person Person { get; set; }
}
public class Person
{
public virtual Klas Klas { get; set; }
public int Id { get; set; }
public string Voornaam { get; set; }
public string Familienaam { get; set; }
public bool Vdab { get; set; }
}
public class Klas
{
public int Id { get; set; }
public string Omschrijving { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("ConnectionString")
{
}
}
RegisterViewModel: RegisterViewModel:
public class RegisterViewModel
{
[Required]
[Display(Name = "Gebruikersnaam")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Het {0} moet misntens {2} Karakters lang zijn.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Wachtwoord")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Bevestig Wachtwoord")]
[Compare("Password", ErrorMessage = "De wachtwoorden komen niet overeen")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Voornaam")]
public string Voornaam { get; set; }
[Required]
[Display(Name = "Familienaam")]
public string Familienaam { get; set; }
[Required]
[Display(Name = "Klas")]
public string Klas { get; set; }
[Required]
[Display(Name = "Vdab Student")]
public bool Vdab { get; set; }
}
And in the AcountController: 并在AcountController中:
[AllowAnonymous]
public ActionResult Register()
{
var Vdab = new SelectList(new[] { new { ID = "1", Name = "Ja" }, new { ID = "0", Name = "Nee" }, }, "ID", "Name", 1);
ViewBag.Vdab = Vdab;
return View();
}
And finally I show the dropdown in the view with razor 最后,我用剃刀在视图中显示下拉菜单
<div class="form-group">
@Html.LabelFor(m => m.Vdab, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("Vdab", @ViewBag.Vdab as SelectList, "----Maak een keuze ----")
</div>
</div>
You can use: 您可以使用:
@Html.DropDownList("Vdab", @ViewBag.Vdab as SelectList, "----Maak een keuze ----")
Just in your controller you could do like this: 只要在您的控制器中,您就可以这样做:
[AllowAnonymous]
public ActionResult Register()
{
List<SelectListItem> myList =new List<SelectListItem>();
myList.Add(new SelectListItem { Value = bool.TrueString, Text = "Ja" });
myList.Add(new SelectListItem { Value = bool.FalseString, Text = "Nee" });
var Vdab = new SelectList(myList,"Value","Text");
ViewBag.Vdab = Vdab;
return View();
}
Add this to your RegisterViewModelClass: 将此添加到您的RegisterViewModelClass:
public IEnumerable<SelectListItem> VdabSelectList { get; set; }
And in the controller: 并在控制器中:
[AllowAnonymous]
public ActionResult Register()
{
RegisterViewModel viewModel = new RegisterViewModel();
viewModel.VdabSelectList = new[]
{
new SelectListItem { Value = bool.TrueString, Text = "Ja" },
new SelectListItem { Value = bool.FalseString, Text = "Nee" }
}
return View(viewModel);
}
[HttpPost]
public ActionResult Register(RegisterViewModel _viewModel)
{
//Do stuff with form data...
}
Finally, add this to your view: 最后,将此添加到您的视图:
<div class="form-group">
@Html.LabelFor(m => m.Vdab, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.vDab, new SelectList(m.VdabSelectList, "Value", "Text"))
</div>
</div>
Disclaimer: I haven't tried a DropDownListFor with a boolean, but based on this post , it looks like it should work. 免责声明:我还没有尝试使用带有布尔值的DropDownListFor,但是根据这篇文章 ,它似乎应该可以工作。 I always use radio buttons for booleans, like this: 我总是将单选按钮用于布尔值,如下所示:
@Html.RadioButtonFor(m => m.Vdab, true)Ja
@Html.RadioButtonFor(m => m.Vdab, false)Nee
Your problem is simply that you have specified that your model type is bool. 您的问题很简单,您已指定模型类型为bool。 Bool is a value type, and is always going to contain a value, thus validation can never fail, no matter what. 布尔是一种值类型,并且始终会包含一个值,因此无论如何验证都不会失败。 It always contains at least the default value, which is false. 它始终至少包含默认值false。
You want to make the type nullable. 您想使类型为可空。
[Required]
[Display(Name = "Vdab Student")]
public bool? Vdab { get; set; }
Now, your required validation will work correctly, and your original (and correct) Html helper will work. 现在,您所需的验证将正常工作,并且原始(且正确)的HTML帮助程序也将工作。
@Html.DropDownFor(x => x.Something, Model.SomethingList, "Select a component type" )
Word of warning, though.. Change your list to Model.SomethigList, using the same name for your Selected item and list can cause a lot of problems. 但是,请注意。将您的列表更改为Model.SomethigList,对您的Selected项和列表使用相同的名称可能会导致很多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.