This project is read-only.

"New" in property name breaks model binding


Consider this code:

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();

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

    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...



MarvinOslo wrote Sep 20, 2012 at 12:13 PM

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 12:13 PM

*I'm seeing

eilonlipton wrote Oct 3, 2013 at 7: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.