I am very new in MVC and my previous experience was asp.net web forms, now i am getting difficulty to add Validation for @Html.DropDownList and @Html.DropDownListFor in Razor using C# asp.net mvc code first approach
Below is my design:
<div class="form-group">
@Html.LabelFor(model => model.State, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("States", new SelectList(ViewBag.States as System.Collections.IEnumerable, "StateId", "StateName", new { @class = "control-label col-md-2" }), "Select a State", new { id = "dd_State" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(Model => Model.City, new SelectList(Enumerable.Empty<SelectListItem>(), "CityName", "CityName"),"Select a City", new { id = "dd_City" })
<span id="span1" name="span1"></span>
</div>
</div>
Below is my Models code Register.cs and not able to add require fields of State and City
[Table("Register")]
public class Register
{
[Key]
public int Sno { get; set; }
[Required(ErrorMessage = "Name is required.")]
[Display(Name = "Full name")]
public string Fullname { get; set; }
[Display(Name = "Email Id")]
[Required(ErrorMessage = "Email is required.")]
public string EmailId { get; set; }
[Required(ErrorMessage = "Password is required.")]
public string Password { get; set; }
[Required(ErrorMessage = "Mobile is required.")]
public string Mobile { get; set; }
[Required(ErrorMessage = "Address is required.")]
public string Address { get; set; }
public string State { get; set; }
public string City { get; set; }
[Required(ErrorMessage = "Entity is required.")]
public string EntityType { get; set; }
public string Website { get; set; }
public string PinCode { get; set; }
public string accountactivated { get; set; }
}
Also i am not able to add bootstrap css to @Html.DropDownList and @Html.DropDownListFor please check dropdown look.
My State and city model class.