开发者

ASP MVC DropDownList error "Converting to Type"

开发者 https://www.devze.com 2022-12-14 15:37 出处:网络
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:

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>


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.


If you have multiple Forms in your view, make sure you are explicitly setting the 'action' attribute of the Form by using the correct overload of the BeginForm method. That is:

@using (Html.BeginForm("ACTION_METHOD", "CONTROLLER", FormMethod.Post, null))

Instead of this one:

@using (Html.BeginForm())


I think your error may be due to having "Status" somewhere in your code instead of "StatusId." Notice the error says it is trying to convert to Status, not StatusId.

I ran into this on a project of mine. If you're using Entity Framework, and your object has a foreign key, EF is also going to create a reference to the foreign object for you (forgive me if I'm not describing this accurately). When you type "MyObject." in code, Intellisense will provide you with both Status and StatusId.

In my case, I used DropDownListFor() with model.PersonType instead of model.PersonTypeId.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号