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:
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With