.NET MVC model null on POST

I came across a problem whilst building my latest application, when I was sending a model from a create view the model was showing up null on post.

The code that wasn’t working:

public class FileNote
{
public int CaseID { get; set; }
public string Title { get; set; }
public string Note { get; set; }
}

[HttpPost]
public ActionResult CreateFileNote(FileNote note)
{
bool fileNoteAdded = FileAccess.Create(note);
//etc etc...

};

Now when the model arrives the object is null, this is because I’ve named the parameter note¬†which is also the same name as one of properties of FileNote. The model binder gets confused trying to bind the property of the model rather than the model itself. Changing the code to the below fixes the issue.

[HttpPost]
public ActionResult CreateFileNote(FileNote newNote)
{
bool fileNoteAdded = FileAccess.Create(newNote);
//etc etc...

};

2 thoughts on “.NET MVC model null on POST

  1. there seems to be no difference between the two codes

    • Marcos, you’ll notice that in the second block of code the object passed to the method is called newNote rather than note

Leave a Reply

Your email address will not be published. Required fields are marked *