views:

196

answers:

2

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

+1  A: 

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

griegs
Jquery.Form is awesome.
jfar
Yeah it's pretty cool.
griegs
hey thank you for this info, however i'll try to use ajax.beginForm first (better compile time checking) to do this, i'm hoping it would work as advertised, otherwise i think i'm missing something if i don't learn how to do it the way it's intended to be done...
Erx_VB.NExT.Coder
A: 

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?

Erx_VB.NExT.Coder
Why would anyone down vote an answer when vie gone to the effort of providing the answer for anyone that might find it useful, I could have just said nothing and left it this way, it doesn't benefit me to provide the answer. I think SO has a lot of programmer snobs who get a kick out of trying to belittle others while trying to look superior to others in an attempt to satisfy their insecurities in other parts of their lives, after 60 questions, I see this all over SO really, overly snobby programmers that are just full of themselves. What does this say about programmers in general? Curious!
Erx_VB.NExT.Coder