I'm just trying to get some simple model binding to work!
My model, defined using Entity Framework, has a property named "Note".
public partial class ContractNote
{
public short Id { get; set; }
public short ContractId { get; set; }
public System.DateTime Date { get; set; }
public string Note { get; set; }
public Nullable<System.DateTime> CompletionDate { get; set; }
public bool Completed { get; set; }
}
I'm using an Ajax call to post the updated data back from a modal form on the web page.
$.ajax({
url: '@urlHelper.Action("EditNote", "Contracts")',
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify({ Id: noteId, ContractId: contractId, Date: date, Note: note, CompletionDate: cdate, Completed: completed }),
success: function (data) {
switch (data.status) {
case "error":
alert(data.message);
break;
default:
Refresh(model.notes);
break;
}
},
And the controller action tries to bind to a ContractNote object.
public JsonResult EditNote(ContractNote note)
{ .... }
I cannot get this to work - the bound object "note" is always null. I found I could post all of the fields OK, except for "Note". If I leave this out it binds OK.
Moreover, I created a dummy class in my code, exactly the same as ContractNote, but with the property "Note" renamed to "xyz".
public class ContractNoteJSON
{
public short Id { get; set; }
public short ContractId { get; set; }
public System.DateTime Date { get; set; }
public string xyz { get; set; }
public Nullable<System.DateTime> CompletionDate { get; set; }
public bool Completed { get; set; }
}
And I updated my controller and Ajax code accordingly.
Incredibly, this binds perfectly! So it would seem that the MVC model binder has problems with a field named "Note" specifically.
Can someone confirm this is the case? Is there a way I can work around this? I am able to change database and EF to give it a different name, but I'd rather not and I'm curious to find out whether some simple mapping can be done.
Thanks in advance
Doug