MVC2 - Ghost Model Error

My Model:

public class EntryModel
{             
        [Required]
        public string Date { get; set; }

        public string Car { get; set; }

        public string Engine { get; set; }

        public string User { get; set; }

        public int CarCod { get; set; }
        public int EngineCod { get; set; }

    }

My View:

<%=Html.Label("Car")%>
<%=Html.Text("Car", car,new { @class = js_car")%>
<%=Html.ValidationMessageFor(x=>x.Car) %>
<%=Html.TextBox("CarCod", carCod, new { @class = "js_car", @style = "display:none;",@id="_car_cod" })%>

My Problem:

When posting the data, "CarCod" always fires an error at the ModelState, thus rendering both ModelState.IsValid and Html.ValidationSummary useless.

I already have workarounds for this (i.e not using them), but it would be very nice to know why.

Answers


To go off of what Garret said, make CarCod and EngineCod nullable.


It looks like you're hiding the CarCod textbox so it will be submitted with an empty string. The Model expects it to be an int so it's having problems converting that to an integer. What happens if you set the value to "0"?


Need Your Help

Oracle 11g: union all with dbms_random

sql function stored-procedures oracle11g

I'm trying to write sql statement in oracle 11g that will randomly select 5000 records from union of two different tables with same columns: