last day (15 days later) » 

9:26 PM
0
A: Custom model binding not being executed

Reza AghaeiHere is my MyModelBinder taht works properly: public class MyModelBinder:DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { //... } protected override void BindProperty(ControllerContext control...

 
Maybe it's the BindProperty that I need?
 
@BanksySan I think so.
 
You're using the DefaultModelBinder, I'm implementing IModelBinder. I'll give your way another go.
Which namespace is the one you're using?
 
@BanksySan using System.Web.Mvc;
 
Cheers, how are you registering the bindings?
 
9:26 PM
@BanksySan I edited the answer and add registration code.
 
Cheers, that's the same as I have. Also in Global.asax in the Application_Start() method.
 
Everything look like normal. Did you try with inherit from DefaultModelBinder?
 
Aye, I've changed it to inherit from it rather than implementing IModelBinding
 
MyModelBinder inherits from DefaultModelBinder.
 
I have this:
` public class DateTimeModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

DateTime dateTime;

var isDate = DateTime.TryParse(value.AttemptedValue,
Thread.CurrentThread.CurrentUICulture,
DateTimeStyles.None,
out dateTime);

if (!isDate)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
$"Cannot bind value {value.AttemptedValue} to a DateTime");
The one in System.Web.Mvc
There are breakpoints in there, but the first breakpoint hit is one in my Controller, after binding has taken place.
 
9:33 PM
Put your datetime field in a model.
 
It is, here's my model:
public class LedgerEntryRequestModel : ILedgerEntryRequest
{
public string Type { get; set; }
public string Direction { get; set; }
public string Method { get; set; }
public string Account { get; set; }
public string Department { get; set; }
public string Cost { get; set; }
public decimal Vat { get; set; }
public string Description { get; set; }
public DateTime? PaidDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
The attributes are there because I've been seeing if any of them will make any difference.
 
Here is my DateTime ModelBinder that works properly:
public class PersianDateTimeToDateTimeModelBinder:DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(Nullable<DateTime>))
{
var name = bindingContext.ModelName;
try
{
if (string.IsNullOrEmpty(bindingContext.ValueProvider.GetValue(name).AttemptedValue))
{
return null;
}
else
{
DateTime dt = PersianDateTime.Parse(bindingContext.ValueProvider.GetValue(name).AttemptedValue).DateTime;
You can use it for test
For test date, use 1394/07/05
it should convert it to 2015/09/27
 
Trying it now, thanks!
 
that is a model binder that converts Persian Date to Julian Date
Gregorian Date
 
Nope, skips the binder completely.
Could you do a search on your project to ensure that it's not referenced in some sneekey place?
 
9:40 PM
I am pretty sure this is the whole story.
Maybe you should exit IISExpress and clean and rebuild your application
It is using in many projects
 
I'll give turning it off an on again a try.
 
I am using this model binder for both DateTime and Nullable<DateTime>
 
Same here.
It looks like your doing exactly what I'm trying to do.
Do breakpoint work in your binding?
I'm wondering if the breakpoints simply don't work on binders.
 
Are you sure your codes in Application_Start run?
 
Are, I can step through the statement that adds the binder to the ModelBinders.Binders collection.
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
Debugger.Launch();

AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

}
}
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            Debugger.Launch();
            ModelBinders.Binders[typeof(DateTime)] = new PersianDateTimeToDateTimeModelBinder();

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
    }
Ignore that first one, accidently deleted a line.
 
10:00 PM
Any success?
 
Nope, nothing.
 

last day (15 days later) »