私のカスタム ASP.NET MVC ModelBinder では、タイプ MyType のオブジェクトをバインドする必要があります。
public class MyType
{
public TypeEnum Type { get; set; }
public string Tag { get; set; } // To be set when Type == TypeEnum.Type123
}
上記の疑似コードでは、「Type」が Type123 の場合にのみプロパティ「Tag」を設定する必要があることがわかります。
私のカスタム ModelBinder は次のようにロックします。
public class CustomModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext cc, ModelBindingContext mbc, PropertyDescriptor pd)
{
var propInfo = bindingContext.Model.GetType().GetProperty(propertyDescriptor.Name);
switch (propertyDescriptor.Name)
{
case "Type": // ....
var type = (TypeEnum)controllerContext.HttpContext.Request.Form["Type"].ToString();
propInfo.SetValue(bindingContext.Model, name, null);
break;
case "Tag": // ...
if (bindingContext.Model.Type == TypeEnum.Type123) { // Fill 'Tag' }
break;
}
}
私が抱えている問題は、私のcurstom ModelBinderでは、プロパティがASP.NET MVCによってバインドされる順序を制御できないことです。
ASP.NET MV によって満たされるプロパティの順序を指定する方法を知っていますか?