1

所以在我的模型中,我有:

public int Number {get; set;}

在我看来,我称之为:

@Html.TextBoxFor(m => m.Number, new { id = "Number" })

当我运行应用程序时,TextBox 中有 0 值。

我试图改变这样的模型:

public int? Number {get; set;}

但后来我得到了例外......但是我的模型中有

public decimal? Price {get; set;}

在我看来:

@Html.TextBoxFor(m => m.Price, new { id = "Price" })

当我运行这个文本框是空的,一切正常。有人解释为什么 int 在这种情况下不能是可为空的类型吗?我得到的例外是Method not found: 'Int32'

目标不是在文本框中有 0 值,而是在普通的空文本框中

4

1 回答 1

0

尝试在您的视图中添加验证,如下所示:

@Html.TextBoxFor(m => m.Price, new { id = "Price" }) @Html.ValidationMessageFor(m => m.Price)

在您的控制器中,您必须验证您的模型状态是否有效。

 If(ModelState.IsValide)
{
// code if all is ok
}
else
{
ModelState.AddModelError("", "Error Message");
                  return View(Model);
}
于 2012-04-18T12:18:34.643 回答