【问题标题】:MVC2 - Ghost Model ErrorMVC2 - 鬼模型错误
【发布时间】:2011-11-30 17:37:11
【问题描述】:

我的模特:

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 都无用。

我已经有解决方法(即不使用它们),但很高兴知道原因。

【问题讨论】:

    标签: c# asp.net-mvc validation asp.net-mvc-2


    【解决方案1】:

    为了摆脱 Garret 所说的,将 CarCodEngineCod 设为可空。

    【讨论】:

      【解决方案2】:

      您似乎隐藏了 CarCod 文本框,因此它将使用空字符串提交。该模型希望它是一个int,因此在将其转换为整数时遇到了问题。如果将值设置为“0”会怎样?

      【讨论】:

      • 我想我们俩实际上都解决了这个问题,但可惜只有一个可以标记为答案。丹尼尔的回答最终是正确的做法,我也建议这样做。
      猜你喜欢
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多