Does MVC 2.0 model binding work with Ajax requests?
i'm just sending a normal POST request using Ajax.BeginForm... i output the form elements using the .TextBoxFor and .HiddenFor etc... all as i should... and when it's posted via ajax to my action method, the object in the action method (named "Comment") is not populated with the values!
Am i missing something? here is the relevant part of my code to those who want to see it...
<% Using Ajax.BeginForm("UpdateComment", "Home",
New AjaxOptions With {.UpdateTargetId = Model.CommentDivId,
.HttpMethod = FormMethod.Post})%>
and....
<%= Html.Hidd开发者_Python百科enFor(Function(x) x.Comment.CommentID)%>
<%= Html.TextAreaFor(Function(x) x.Comment.Comment, 8, 40,
New With {.style = "overflow: hidden;"})%>
<%= Html.ValidationMessageFor(Function(x) x.Comment.Comment) %>
here is the Action Method, which raises the error... the error is a null reference exception when i try to use the object:
Function UpdateComment(ByVal UpCom As Comment) As ActionResult
Dim db = New FPicDataContext Dim Updatable = (From c In db.Comments Where c.CommentID = UpCom.CommentID).FirstOrDefault Updatable.Comment = UpCom.Comment ' THIS IS WHERE THE OBJECT IS NULL ERROR IS RAISED! BASICALLY, ALL THE VALUES IN UPCOM (AS COMMENT) ARE 0 OR NOTHING. db.SubmitChanges()
Dim cm = New CommentModel With {.Comment = UpCom, .CommentDivId = "CommentDiv" & UpCom.CommentID.ToString}
Return PartialView("Comment", cm)
End Function
this problem i eventually solved, turns out object name that model is bound to in action argument must be the same name as the object name you used when doing the TextBoxFor BeginForm etc... tested, confirmed, that was it!
so, in other words, UpCom
had to be named Comment
instead :).
however, a note of caution, i have not heard about this requirement anywhere, in any documentation or anything!! anyone have any thoughts about this?
Wait I think I see what you're trying to do now and the answer is yes absolutaly.
There are a couple of jQuery plugins you can grab that will allow the posting of forms using Ajax.
I've used this one and it works fine. jQuery Form Plugin This one might also work for you. .submit
@Erx_VB.NExt.Coder I gave you back the point because I stumbled across this looking for something else and eventually hit the same issue, but the wording wasn't the clearest so I wanted to update and try to help. In my example I iterate through a Findings object via
foreach ( var row in Model.Findings ) /* Doesn't Work */
.
.
.
<td> <%: Html.TextAreaFor(m => row.Description)%> </td>
and my controller:
public void Update(ReportFindingViewModel Finding)
and print fields out inside a table. I was getting hits back to the Action whenever I post, but like you said, the object was null:
<%: Ajax.BeginForm("Update", "ReportFinding", new AjaxOptions { HttpMethod = "Post" } )%>
As you explained, it was all in the name, but what I want to clarify is that it's the name of the lambda expression that you bind to in the fields. When I changed it to:
foreach ( var Finding in Model.Findings ) /* Does Work */
.
.
.
<td> <%: Html.TextAreaFor(m => Finding.Description)%> </td>
the ModelBinding auto-magically figured it all out. I'm completely new to using the included ms ajax functionality and pretty new to mvc altogether, but this helped. Thanks!
精彩评论