1
Vote

"New" in property name breaks model binding

description

Consider this code:

[HttpPost]
public ActionResult KundeKontakt(NewKundeKontaktsModel newKontakts)
{
// newKontakts is always empty !!!
// (all values are initialized to their constructor defaults)
}
public class NewKundeKontaktsModel
{
    public NewKontaktsForKunde NewKontakts { get; set; } // <---- HERE !!!!!!

    public IEnumerable<SelectListItem> KundeList { get; set; }

    public NewKundeKontaktsModel()
    {
        KontaktsToAdd = new NewKontaktsForKunde();
    }
}

[DataContract]
public class NewKontaktsForKunde
{
    [DataMember]
    [Display(Name = "Kunde")]
    [Required(ErrorMessage = "Vennligst velg kunde")]
    public int KundeId { get; set; }

    [DataMember]
    public List<NewKundeKontakt> KundeKontakts { get; set; }

    public NewKontaktsForKunde()
    {
        KundeKontakts = new List<NewKundeKontakt> {new NewKundeKontakt { Mandatory = true } };
    }
}
In my controller action, all values are empty - obviously, the constructor is invoked, but that's it.
I spent DAYS tracking down this issue (there were no compile errors or exceptions) - then I decided to justs rename the property from "NewKundeKontaktsModel.NewKontakts" to "NewKundeKontaktsModel.KontaktsToAdd" - suddenly everything worked!!

It looks like the DefaultModelBinder has some issues with keywords in property names - or at least the word "New".

This looks like a bug to me, just thought I'd let you know...

Thanks,
Marvin

comments

MarvinOslo wrote Sep 20, 2012 at 11:13 AM

Oops, I'm saying that in the above code it already says "KontaktsToAdd = new NewKontaktsForKunde();" - that would of course have been "NewKontakts = new NewKontaktsForKunde();" in the "broken" version (like I said, everything compiled and ran just fine - the model was just empty...)

MarvinOslo wrote Sep 20, 2012 at 11:13 AM

*I'm seeing

eilonlipton wrote Oct 3, 2013 at 6:43 PM

It seems unlikely to me that the prefix "New" is related to this. We should try to repro this and post the data and see what happens.