Question

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?)

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:

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:

      [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>
Was it helpful?

Solution

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();
}

OTHER TIPS

Add this to your 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. 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 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.

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.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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top