Reputation: 11165
I have this Controller:
[HttpPost]
public JsonResult Execute(PaymentModel paymentModel){...}
this is the model
public class PaymentModel
{
[Required]
[DisplayName("Full name")]
public string FullName { get; set; }
...
}
this is the binding action
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.Add(typeof(PaymentModel), new PaymentModelsBinding());
}
this is the binding inplementation
public class PaymentModelsBinding : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//Cant get to here with the debugger
}
I dont know if that is relevant or not, but I am injecting using Ninject to the controller constructor.
Update This is how the form is submitted:
$.ajax({
type: 'POST',
url: $("#form").attr("action"),
data: $("#form").serialize(),
success: function (json) {
...
},
dataType: "Json"
});
I want that to be restful, meaning I will to call it in every possible WEB way.
Browser Ajax, Browser Classic form submission, WebClient... and more.
Update This is my ninject code:
kernel.Components.Add<IInjectionHeuristic, CustomInjectionHeuristic>();
kernel.Bind<IPaymentMethodFactory>().ToProvider<PaymentMethodFactoryProvider>().InSingletonScope();
kernel.Bind<IDefaultBll>().To<DefaultBll>().InSingletonScope();
kernel
.Bind<IDalSession>()
.ToProvider<HttpDalSessionProvider>()
.InRequestScope();
Thanks
Upvotes: 0
Views: 581
Reputation: 1038930
Sorry, I can't see anything wrong with your code. This should work. And as a proof of concept, here's what you could try:
Define a view model:
public class PaymentModel
{
[Required]
[DisplayName("Full name")]
public string FullName { get; set; }
}
A custom model binder:
public class PaymentModelsBinding : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return new PaymentModel();
}
}
HomeController:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new PaymentModel());
}
[HttpPost]
public ActionResult Index(PaymentModel model)
{
return Json(new { success = true });
}
}
A corresponding view (~/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>
And finally register the model binder in Application_Start
:
ModelBinders.Binders.Add(typeof(PaymentModel), new PaymentModelsBinding());
Run the application in Debug mode, submit the form and the custom model binder gets hit.
So the question now becomes: what did you do differently?
Upvotes: 1