2013-01-13 2 views
0

MVC 3에 발사하지 :IModelBinder 내가이 컨트롤러가

[HttpPost] 
public JsonResult Execute(PaymentModel paymentModel){...} 

이 모델입니다

public class PaymentModel 
{ 
[Required] 
[DisplayName("Full name")] 
public string FullName { get; set; } 
... 
} 

이 이것을 inplementation

결합되어있는 바인딩 작업을

protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
      ModelBinders.Binders.Add(typeof(PaymentModel), new PaymentModelsBinding());   
     } 

입니다

public class PaymentModelsBinding : IModelBinder 
    { 
     public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
     { 
//Cant get to here with the debugger 
} 

관련이 있는지 없는지는 잘 모르겠지만 Ninject를 컨트롤러 생성자에 주입하고 있습니다.

업데이트 이 양식을 제출하는 방법입니다

  $.ajax({ 
       type: 'POST', 
       url: $("#form").attr("action"), 
       data: $("#form").serialize(), 
       success: function (json) { 
        ... 

       }, 
       dataType: "Json" 
      }); 

내가 그 편안한되고 싶어요, 모든 가능한 웹 방식으로 전화를 나는 것 의미한다.
브라우저 Ajax, 브라우저 기본 양식 제출, WebClient ... 등.

업데이트 이 내 Ninject에 코드입니다 :

kernel.Components.Add<IInjectionHeuristic, CustomInjectionHeuristic>(); 


      kernel.Bind<IPaymentMethodFactory>().ToProvider<PaymentMethodFactoryProvider>().InSingletonScope(); 
      kernel.Bind<IDefaultBll>().To<DefaultBll>().InSingletonScope(); 

      kernel 
       .Bind<IDalSession>() 
       .ToProvider<HttpDalSessionProvider>() 
       .InRequestScope(); 

감사

+0

이 컨트롤러 동작을 어떻게 부르겠습니까? 액션이 호출 될 때 모델 바인더가 호출됩니다. –

+0

@DarinDimitrov - 웹 양식을 통해 전화하고 있습니다. 'Execute (PaymentModel paymentModel)'을 호출 할 수 있지만 그 전에는 모델을 호출 할 수 없습니다. – SexyMF

+0

어떻게 부르시겠습니까? 제출 된보기 내부에 HTML 양식이 있습니까? 아니면 AJAX를 사용하고 있습니까? 아니면 다른 방법으로이 작업을 호출 할 수 있습니까? –

답변

1

죄송합니다, 나는 당신의 코드를 잘못 아무것도 볼 수 없습니다. 이것은 효과가있다. 그리고 개념의 증거로, 여기 당신이 시도 할 수있는 작업은 다음과 같습니다

public class PaymentModel 
    { 
     [Required] 
     [DisplayName("Full name")] 
     public string FullName { get; set; } 
    } 
  • A :

    1. 는 뷰 모델을 정의 인터넷 템플릿을 사용하여 새 ASP.NET MVC 3 응용 프로그램을 만듭니다 사용자 정의 모델 바인더 :

      public class PaymentModelsBinding : IModelBinder 
      { 
          public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
          { 
           return new PaymentModel(); 
          } 
      } 
      
    2. HomeController :

      public class HomeController : Controller 
      { 
          public ActionResult Index() 
          { 
           return View(new PaymentModel()); 
          } 
      
          [HttpPost] 
          public ActionResult Index(PaymentModel model) 
          { 
           return Json(new { success = true }); 
          } 
      } 
      
    3. 상응하는 뷰 (~/Views/Home/Index.cshtml)

      @model PaymentModel 
      
      @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" })) 
      { 
          @Html.EditorFor(x => x.FullName) 
          <button type="submit">OK</button> 
      } 
      
      <script type="text/javascript"> 
          $('#form').submit(function() { 
           $.ajax({ 
            type: this.method, 
            url: this.action, 
            data: $(this).serialize(), 
            success: function (json) { 
      
            } 
           }); 
           return false; 
          }); 
      </script> 
      
    4. 마지막 Application_Start에서 모델 바인더 등록 :

      ModelBinders.Binders.Add(typeof(PaymentModel), new PaymentModelsBinding()); 
      
    5. 실행 디버그 모드에서 애플리케이션 양식을 제출 커스텀 모델 바인더가 히트됩니다.

    이제 질문은 달라집니다. 어떻게 다르게 했습니까?

  • +0

    방금 ​​해 봤는데 일하고 있어요. 그게 문제가 될 수 있습니까? 내 게시물을 편집하여 하단에 추가했습니다. – SexyMF

    +0

    @SexyMF, 예, 많은 일이 발생할 수 있습니다. 불행히도 나는 신탁이 아니며 가상으로 작성한 코드를 원격으로 읽거나 이해할 수 없습니다. 그러나 이것이 NInject에 의한 것 같지는 않습니다. 원인 제거를 시작하는 것이 좋습니다. 작동 상태 (내 대답 참조)에서 시작한 다음 코드가 작동을 멈출 때까지 점진적으로 물건을 추가하십시오. 이제 문제를 격리하고 흡연 총을 발견했을 것입니다. 행운을 빕니다. –

    +0

    감사합니다. – SexyMF