I am a beginner in MVC and trying to bind drop-down list to a model using data from a database lookup table.
My Model:
public class State
{
[Required]
public string StateName { get; set; }
public int CountryId { get; set; }
public List<SelectListItem> CountryList { get; set; }
}
In my Controller:
[HttpGet]
public ActionResult AddState()
{
State obj = new State();
using (MvcWorkEntities context = new MvcWorkEntities())
{
obj.CountryList = context.Mst_Country.ToList();
}
return View(obj);
}
The View:
@using(Html.BeginForm("AddState","Home",FormMethod.Post))
{
<div class="col-md-4">
<div class="form-group">
@Html.Label("Select Country")
@Html.DropDownListFor(m=>m.CountryId,Model.CountryList);
</div>
</div>
<div class="col-md-4">
<div class="form-group">
@Html.Label("Enter State Name")
@Html.TextBoxFor(m=>m.StateName)
</div>
</div>
}
This isn't working as intended and I don't understand where I am going wrong. Could anyone point out where I might be making a mistake?