2011-11-30 34 views
0

我的模型:MVC2 - 鬼模型錯誤

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; } 

    } 

筆者認爲:

<%=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" })%> 

我的問題:

當發佈的數據, 「CarCod」 總是在ModelState中觸發一個錯誤,因此渲染ModelState.IsValid和Html.ValidationSummary都是無用的。

我已經有這方面的解決方法(即不使用它們),但它會很高興知道爲什麼。

回答

1

要加勒特說什麼,使CarCodEngineCod可以爲空。

1

它看起來像你隱藏的CarCod文本框,所以它會被提交一個空字符串。該模型預計它是一個int所以它有問題轉換爲一個整數。如果將該值設置爲「0」會發生什麼?

+0

很難決定接受哪個答案,也謝謝。 –

+0

我認爲我們都解決了這個問題,但是隻有一個可以被標記爲答案。丹尼爾的回答最終是正確的,我也會推薦。 –