【问题标题】:ReadOnly(true) property's value not binded in Model BindingReadOnly(true) 属性的值未绑定在模型绑定中
【发布时间】:2012-03-14 17:29:04
【问题描述】:

我的一个属性标有 ReadOnly(true) 属性,在模型的添加视图中,我为此属性分配了一个默认值,在我的例子中是 DateTime.Now。

问题在于,在 Add 的 POST 操作中,模型正常到达,除了 ReadOnly(true) 属性,它到达 1/1/0001(它是 DateTime)。

我能做些什么来解决它?我确实想获得价值。

谢谢

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-3 c#-4.0 model-binding


【解决方案1】:

来自“OdeToCode.com”:

如果您不想使用 ReadOnly 元数据,则可能需要使用 绑定 IsAdmin 属性。 (注意:我记得 ReadOnly 不工作 MVC 2 或 MVC 1,但它在 3 和 4(测试版)中工作)。公共类用户 {

public string FirstName { get; set; }

[ReadOnly(true)]
public bool IsAdmin { get; set; } }

【讨论】:

  • 这解释了如何避免模型绑定值,同时也将它们标记为只读。这与问题所要求的相反。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
相关资源
最近更新 更多