Asp.net mvc 3 IModelBinder未在MVC 3上点火
我有一个控制器:Asp.net mvc 3 IModelBinder未在MVC 3上点火,asp.net-mvc-3,ninject,model-binding,Asp.net Mvc 3,Ninject,Model Binding,我有一个控制器: [HttpPost] public JsonResult Execute(PaymentModel paymentModel){...} public class HomeController : Controller { public ActionResult Index() { return View(new PaymentModel()); } [HttpPost] public ActionResult Ind
[HttpPost]
public JsonResult Execute(PaymentModel paymentModel){...}
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new PaymentModel());
}
[HttpPost]
public ActionResult Index(PaymentModel model)
{
return Json(new { success = true });
}
}
这就是模型
public class PaymentModel
{
[Required]
[DisplayName("Full name")]
public string FullName { get; set; }
...
}
这就是约束作用
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"
});
我希望它是restful的,这意味着我将以各种可能的网络方式调用它。浏览器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();
kernel.Components.Add();
kernel.Bind().ToProvider().InSingletonScope();
kernel.Bind().To().InSingletonScope();
内核
.Bind()
.ToProvider()
.InRequestScope();
谢谢对不起,我看不出你的代码有什么问题。这应该行得通。作为概念证明,以下是您可以尝试的:
public class PaymentModel
{
[Required]
[DisplayName("Full name")]
public string FullName { get; set; }
}
public class PaymentModelsBinding : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return new PaymentModel();
}
}
[HttpPost]
public JsonResult Execute(PaymentModel paymentModel){...}
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new PaymentModel());
}
[HttpPost]
public ActionResult Index(PaymentModel model)
{
return Json(new { success = true });
}
}
~/Views/Home/Index.cshtml
):
现在的问题是:您做了哪些不同的操作?您如何调用此控制器操作?调用操作时将调用模型绑定器。@DarinDimitrov-我是通过web表单调用的。我可以调用
Execute(PaymentModel PaymentModel)
,但不能调用前面的模型。您能告诉我如何调用它吗?在提交的视图中是否有HTML表单?或者您正在使用AJAX?或者用其他方式来调用这个动作?@DarinDimitrov-我已经编辑了代码。谢谢,我刚刚做了,它正在工作。这会是一个笑话吗?我编辑了我的帖子,并在bottom@SexyMF是的,很多事情都会导致这种情况。不幸的是,我不是一个甲骨文,无法远程阅读或理解您假设编写的代码。但这不太可能是由NInject造成的。我建议你开始消除这些原因。从工作状态开始(见我的答案),然后逐步开始添加内容,直到代码停止工作。现在,您将隔离问题并找到冒烟的枪。祝你好运