I want to have a drop down list with 3 values (super user-admin-user) this is the role
this is my Model:
public class LogIn { public int ID { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [Display(Name = " username")] public string userame { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = " password")] public string password { get; set; } public string role { get; set; } }and this is my view:
@using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>LogIn</h4> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.LabelFor(model => model.userame, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.userame) @Html.ValidationMessageFor(model => model.userame) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.password, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.password) @Html.ValidationMessageFor(model => model.password) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.role, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.role) @Html.ValidationMessageFor(model => model.role) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }The role is not supposed to be a text field it is supposed to be a drop down list of the three values above and the selected value will be saved in the database.how is that possible?
Hi,
Remove EditorFor() for role and use dropdownList:
@Html.DropdownListFor(model => model.role, new SelectList(new[]{"super user","admin","user"}))