MVC Controller Not Binding Id Param On Post
I have a Model that has several properties. When I submit a form, I pass along the values for those properties (generated by the jQuery $.serialize()
method) and they are automatically bound. This works without a problem. However, now I want to add an Id
to the string, and I have to do it manually since it's not a form field. I've done so by appending '&Id=' + myId
to the end of the URL being posted. The result is a fine query string (I've checked what's getting sent in Firebug and a valid ID is getting posted)
My Model has a property Id
:
public Int32 Id { get; set; }
One would think that the Id would get bound automatically in the controller, but even though all the other data is, the Id is not. This is really fru开发者_运维技巧strating. What could I possibly be doing wrong?
I found the answer to this problem, and given the details I posted in the question, no one would have been able to answer it. I'm posting the answer because I feel like it could potentially help someone.
In my Model, my Id property has a few decorations that I left out because I didn't think they were important, but it turns out they are. Here's what my whole property looks like:
[DataMember(Name = "id")]
[Column(Name = "id", AutoSync = AutoSync.OnInsert, IsDbGenerated = true,
IsPrimaryKey = true)]
public Int32 Id { get; set; }
Apparently, the AutoSync
part of the decoration means that it syncs with the database and cannot actually be set declaratively. So if you're trying to do what I did, make sure you don't have AutoSync
declared!
Hope this helps someone :)
精彩评论