I have code for a DropDownList in C# with MVC. And the value of that is passed as a string. But, I want that as an integer. How should I convert it on the controller before SaveChanges()
, because it is throwing an error.
Here is my View code:
<div class="form-group">
@Html.LabelFor(model => model.DesignationID, "DesignationID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DesignationID", new SelectList(ViewBag.Designationid, "Value", "text"),"Select Designation", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DesignationID, "", new { @class = "text-danger" })
</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>
Here is my Controller code:
// POST: Employee/Create
[HttpPost]
public ActionResult Create(Employee employee)
{
try
{
employeeService.Insert(employee);
unitOfWork.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception ex)
{
return View(ex);
}
}
Here is the error:
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbUpdateException', but this dictionary requires a model item of type 'Pal.Entities.Models.Employee'.
Please help me to resolve this.