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.HiddenFor(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,
UpComhad to be namedCommentinstead :).however, a note of caution, i have not heard about this requirement anywhere, in any documentation or anything!! anyone have any thoughts about this?