tags:

views:

497

answers:

1

Hey, I have a table with 2 fields of type int which are "StatusID" and "TypeID". TypeID works correctly but StatusID returns an error. Here's what my controller looks like:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Project project)
{

    var db = new DB();

    if (ModelState.IsValid)
    {

        try
        {
            db.Projects.InsertOnSubmit(project);
            db.SubmitChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View(project);
        }
    }


    ViewData["Status"] = from c in db.Status
                         select new SelectListItem
                             {
                                 Text = c.Text,
                                 Value = c.StatusID.ToString()
                             };

    ViewData["Types"] = from t in db.Project_Types
                        select new SelectListItem
                            {
                                Text = t.Text,
                                Value = t.TypeID.ToString()
                            };
    return View(project);
}

The error I get is:

Message = "The parameter conversion from type 'System.String' to type 'ConstructionProject.Models.Status' failed because no TypeConverter can convert between these types."

But like I said, the database model for the field "TypeID" and "StatusID" are identical.

Edit:

DB Schema:

Table Project

ProjectID   int
TypeID      int
StatusID    int
Name        varchar(50)
Description text
DateCreated datetime
ClientID    int
Contractor  int

Table Status

StatusID    int
Text        varchar(50)

Table Project Type

TypeID      int
Text        varchar(50)

Anyone knows what would correct the issue here?

My View Page (after changing to StatusID):

        <p>
            <label for="Status">Status:</label>
            <%= Html.DropDownList("StatusID")%>
            <%= Html.ValidationMessage("StatusID", "*")%>
        </p>
        <p>
            <label for="Types">Type:</label>
            <%= Html.DropDownList("Types")%>
            <%= Html.ValidationMessage("Types", "*")%>
        </p>
+4  A: 

Here's a shot in the dark: in your view, you've given the Status select list an ID of "Status", which, when you post a set of form data, causes the default form collection mapping to try to convert the selected value for the list (which is a string version of your int StatusID) into a ConstructionProject.Models.Status object, which fails. Try using "StatusID" for the ID of your select list and see if that works.

For better answers, you should post the relevant parts of your view markup as well.

Mike Powell
After changing it to StatusID, I get: The ViewData item with the key 'StatusID' is of type 'System.Int32' but needs to be of type 'IEnumerable<SelectListItem>'.
AlexDemers
Well, I just changed the name to something completely different and it worked. I guess I'll need to read more on ViewData object. Thanks for the help.
AlexDemers
@AlexDemers please mark this answer as accepted
Jan Jongboom
Alex, I don't think your issue is with ViewData -- your problem is occurring when the user's form input values are being mapped to the Project object. This process is called Model Binding, and here's a great article on it: http://odetocode.com/Blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
Mike Powell