理由:私のコントローラーの1つでは、アプリケーションの残りの部分とは異なる方法ですべてのDecimal値をバインドします。私はDefaultModelBinder
クラスから派生する、私が試してみましたネストされたプロパティ値をバインドするカスタムモデルバインダー
(ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
経由)のGlobal.asaxでのモデルバインダーを登録し、そのBindProperty
メソッドをオーバーライドする必要はありませんが、それは唯一のモデルインスタンスの即時(ネストしていない)小数点の性質のために働きます。
私は私の問題を示すために、次の例があります。
namespace ModelBinderTest.Controllers
{
public class Model
{
public decimal Decimal { get; set; }
public DecimalContainer DecimalContainer { get; set; }
}
public class DecimalContainer
{
public decimal DecimalNested { get; set; }
}
public class DecimalModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.PropertyType == typeof (decimal))
{
propertyDescriptor.SetValue(bindingContext.Model, 999M);
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
public class TestController : Controller
{
public ActionResult Index()
{
Model model = new Model();
return View(model);
}
[HttpPost]
public ActionResult Index([ModelBinder(typeof(DecimalModelBinder))] Model model)
{
return View(model);
}
}
}
このソリューションは、唯一の「999へのDecimal
プロパティが、DecimalContainer
に何もしない」Model
を設定し、S DecimalNested
プロパティ。これは、base.BindProperty
がDecimalModelBinder
のBindProperty
オーバーライドで呼び出されたためですが、小数点プロパティを扱う際に、モデルクラスを使用するように基本クラスを説得する方法がわかりません。 HttpContextにいくつかの値を注入します、その後
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
と(それがモデルバインダーの前に実行されるようはい、許可フィルタ)カスタム認証フィルタを持っていた: