In the code below, I have a DropDownList populated from database records. The DropDownList is populated and displays on the form perfectly fine but when the form is submitted and posted back to the controller I get an error on the following line:
I also receive the following error in the visual studio debug output:
System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to int.
I've double and triple checked the stored procedure's parameters, parameter order, and data types match. This worked the other day and broke at some point when I created a EnumDropDownListFor
. Removing the problematic DropDownListFor
doesn't cause an error on the next EnumDropDownListFor
but causes the same error to occur on the DropDownListFor
tied to Forklfit.LocationID
.
How can I get my regular DropDownLists tied to other object IDs to properly be mapped to my ForkliftViewModel.Forklift
again while using EnumDropDownListFor
for my Forklift enum property?
In the event it helps..
DropDownListFor
elements worked prior to the addition of Forklift.Power
enum property and EnumDropDownListFor
.View: Edit Forklift View
Model: Forklift Model
ViewModel:
public class ForkliftViewModel
{
public Forklift Forklift { get; set; }
public HashSet<Forklift> ForkliftStockInventoryList { get; set; }
public SelectList ManufacturerDropdownList { get; set; }
public SelectList LocationDropdownList { get; set; }
}
Controller Methods:
// GET: Forklift/Edit/ID
public ActionResult Edit(int id)
{
ForkliftViewModel FVM = new ForkliftViewModel();
FVM.Forklift = Forklift.GetForkliftByID(id);
FVM.ManufacturerDropdownList = new SelectList(Manufacturer.GetForkliftManufacturers(), "ManufacturerID", "Name");
FVM.LocationDropdownList = new SelectList(Client.GetAllLocations(), "ClientID", "Name");
return View(FVM);
}
// POST: Forklift/Edit/ID
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ForkliftViewModel FVM)
{
try
{
return RedirectToAction("Details", new { id = Forklift.UpdateForklift(FVM.Forklift).ForkliftID });
}
catch
{
return View();
}
}
EDIT:
I am now certain this error only occurs when attempting to use EnumDropDownListFor
with my Forklift model property as public enum Power Power { get; set; }
. When I replace EnumDropDownListFor
with DropDownListFor
and change the Forklift model type from enum
to int
it works perfectly fine.
User contributions licensed under CC BY-SA 3.0