【发布时间】:2015-10-17 17:50:24
【问题描述】:
我可以在此操作中使用a custom model binder 将0 和1 绑定到false 和true:
[IntToBoolBinder]
public virtual ActionResult foo(bool someValue) {
}
但现在假设参数是一个强类型模型:
public class MyModel {
public int SomeInt { get; set; }
public string SomeString { get; set; }
public bool SomeBool { get; set; } // <-- how to custom bind this?
}
public virtual ActionResult foo(MyModel myModel) {
}
请求将包含int,而我的模型需要bool。我可以为整个MyModel 模型编写一个自定义模型绑定器,但我想要更通用的东西。
是否可以自定义绑定强类型模型的特定属性?
【问题讨论】:
-
如果属性是bool,为什么不使用
@Html.CheckBoxFor(m => m.SomeBool)?为什么请求会包含int? -
@StephenMuecke 我不想要
&foo=True,因为我的用户使用查询字符串尝试不同的组合。当它是 int 时,它们不会。
标签: c# asp.net asp.net-mvc model-binding