This line is causing me some problems in an MVC app i'm developing
<%= Html.DropDownListFor(model => model.TypeID, new SelectList((IEnumerable)ViewData["TaskingTypes"], "TypeID", "TypeName"))%>
It causes the error in the title when two other required fields in the form are not filled in. When the fields are filled in, the form is submitted and recorded in the db, no problems. Anyone have any ideas why the validation is not being picked up and transferred to the view?
'TaskingTypes' is an entity that has a 1 to many relationship with the 'Tasking' entity. The foriegn key in 'Tasking' is 'TypeID'
The top 2 lines of the stack trace are:
[ArgumentNullException: Value cannot be null.
[Parameter name: items]
System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, IEnumerable selectedValues) +262322
System.Web.Mvc.SelectList..ctor(IEnumerable items, String dataValueField, String dataTextField) +31
THis is the controller
[AcceptVerbs(HttpVerbs.Get),Authorize]
public ActionResult Create(){
Tasking tasking = new Tasking()
{
Created_On = DateTime.Now
};
ViewData["TaskingTypes"] = tt.GetAllTaskingTypes().ToList();
return View(tasking);
}
[AcceptVerbs(HttpVerbs.Post),Authorize]
public ActionResult Create(Tasking tasking)
{
if(TryUpdateModel(tasking)){
tasking.Created_On = DateTime.Now;
tasking.Created_By = User.Identity.Name;
taskingRepository.Add(tasking);
taskingRepository.Save();
return RedirectToAction("Details", new { id = tasking.TaskingID });
}
return View(tasking);
}
and this is the Validation class
public class Tasking_Validation
{
[Required(ErrorMessage = "Please select a tasking type")]
public string TypeID { get; set; }
[Required(ErrorMessage = "Tasking Title is Required")]
[StringLength(255, ErrorMessage="Title cannot be longer than 255 characters")]
public string Title { get; set; }
[Required(ErrorMessage = "Location is Required")]
[StringLength(255, ErrorMessage = "Location cannot be longer than 50 characters")]
public string Location { get; set; }
}
Many thanks for looking