2017-06-02 8 views
0
public class LogsController : Controller 
    { 
     public async Task<ActionResult> User(string id, int? page) 
     { 
      return await User(id, (Page)page); 
     } 

     private async Task<ActionResult> User(string id, Page page) 
     { 
      var data = await Repo.GetData(id, page, 10); 
      var model = new UserLogs 
      { 
       User = id, 
       Events = data, 
       Pager = new PagerInput(page, 10, data.TotalCount) 
      }; 

      return View("UserGrid", model); 
     } 
    } 

マイページクラスは、ページが1未満であることを確認するなどの便利な機能を提供します。しかし、私のパブリックアクションがPageをパラメータとして受け取った場合、フォームまたはクエリーストリングの値にかかわらず、intとの暗黙的なキャストを定義しても、常にnullになります。MVC5 - アクションパラメータタイプの逆シリアル化動作を定義するにはどうすればよいですか?

私はMVCにページタイプを逆シリアル化する方法を教えてもらえますか?そのようなメソッド/文字列から変換するいくつかの並べ替えを提供していますか?

複数のコントローラ/アクションで使用するので、実際のページタイプで定義したいと思います。

私はダブルアクションの定義が必要ないといいですね。

答えて

0

このカスタムモデルバインダーは、あなたが定義することができます単純に文字列パラメータを持つコンストラクタを定義することによって、任意の型の非直列化動作TER:

public class CustomModelBinder : DefaultModelBinder 
{ 
    static Assembly _assembly = typeof(CustomModelBinder).Assembly; 
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (_assembly.Equals(bindingContext.ModelType.Assembly)) 
     { 
      var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 
      var ctor = bindingContext.ModelType.GetConstructor(new Type[] { typeof(string) }); 
      if (ctor != null && value != null) 
       return ctor.Invoke(new object[] { value.AttemptedValue }); 
     } 
     return base.BindModel(controllerContext, bindingContext); 
    } 
} 

そして、それを登録...

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    ModelBinders.Binders.DefaultBinder = new CustomModelBinder(); 
} 
1

独自のモデルバインダーを作成する必要があります。 GUIDのため

例:のApplication_Startから

public class BindingConfig 
{ 
    public static void RegisterBinders(ModelBinderDictionary binders) 
    { 
     binders.Add(typeof(Guid), new GuidModelBinder()); 
     binders.Add(typeof(Guid?), new GuidModelBinder()); 
    } 
} 

最後のコールRegisterBinders時:

public class GuidModelBinder : DefaultModelBinder 
{ 
    public override object BindModel(
      ControllerContext controllerContext, 
      ModelBindingContext bindingContext) 
    { 
     if (bindingContext.ModelType == typeof(Guid) || 
      bindingContext.ModelType == typeof(Guid?)) 
     { 
      Guid result; 

      var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 

      if (valueResult.AttemptedValue == null && 
        bindingContext.ModelType == typeof(Guid)) 
       return Guid.Empty; 

      if (valueResult.AttemptedValue == null && 
        bindingContext.ModelType == typeof(Guid?)) 
       return null; 

      if (Guid.TryParse(valueResult.AttemptedValue, out result)) 
       return result; 
     } 

     return base.BindModel(controllerContext, bindingContext); 
    } 
} 

が後バインディング設定クラスを作成

protected void Application_Start() 
{ 
    BindingConfig.RegisterBinders(ModelBinders.Binders); 
} 
+0

おかげで、これは私が、より一般的な解決策に始まりました。 – TheCatWhisperer

関連する問題