DropDownList not mapping to ViewModel properties causing nvarchar to int sql conversion error

0

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:

enter image description here

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..

  1. Error occurs at form submission on all view pages the dropdowns are needed (Edit/Create).
  2. These DropDownListFor elements worked prior to the addition of Forklift.Power enum property and EnumDropDownListFor.
  3. I have tried programmatically creating the select lists in the view but receive the same error.

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.

c#
razor-2
asp.net-mvc-5.2
asked on Stack Overflow May 22, 2018 by Greg Hess • edited May 22, 2018 by Greg Hess

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0