The List of the month are Already added In Databases. For Adding the salary (Create operation), i have to select the month type from the Drop Down List, if the month is not selected the program should not have to redirect to create action. How Can i validate the Drop Down, Before Routing to Create Action ?
@using(Html.BeginForm("Create","Talab",FormMethod.Post))
{
<div class="row">
<div class="form-group">
<div class="col-md-2">
<a href="@Url.Action("Create","TotalSalary")" class="btn btn-success input-sm">Add New </a>
</div>
</div>
<div class="form-group">
<div class="col-md-2">
@Html.DropDownListFor(model => model.Month.id, (IEnumerable<SelectListItem>)ViewData["monthType"], "--Select a Month--")
@Html.ValidationMessageFor(model => model.Month.id)
</div>
</div>
</div>
}
My View Model has following Property
public class Salary
{
public int id { get; set; }
public Nullable<int> month_id { get; set; }
[Required]
public virtual Month Month { get; set; }
public IEnumerable<Month> GetMonths()
{
IEnumerable<Month> mat = null;
mat = this.db.Months.ToList();
return mat;
}
}
Public Class Month
{
public int id { get; set; }
public string month { get; set; }
public virtual ICollection<Salary> Salary { get; set; }
}
My Controller Action Index
public ActionResult Index()
{
Salary salary = new Salary();
ViewData["monthType"] = salary .GetMonths().ToList().Select(
s => new SelectListItem
{
Text = s.month,
Value = s.id.ToString()
});
return View(salary);
}