views:

443

answers:

2

Hi,

I am pretty new to MVC. I have my first Ajax Form here:

<div id="test"></div>
<div id="MainChatMenu">
<% using (Ajax.BeginForm("SendMessage", "MainChat", new AjaxOptions {  UpdateTargetId="test"}))
{ %>
  <input id="chatMessageText" type="text" maxlength="200"  />
<input type="submit" value="Go"/>
<% } %>

Now, if I click the submit button, the page is reloading, goint to mysite/controller/action. I thought that the default behaviour of the Ajax.BeginForm was exactly not to do that? Where's my newbie mistake?

My Controller is called correctly, but data passing also doesn't work. Probably because of the same mistake? Here's the code:

public class MainChatController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public EmptyResult SendMessage(FormCollection formValues)
    {
        return new EmptyResult();
    }

}
A: 
<% using (Ajax.BeginForm("SendMessage", "MainChat", new{}, new AjaxOptions {  UpdateTargetId="test", HttpMethod="POST"})) %>
Gregoire
System.Web.Mvc.Ajax.AjaxOptions does not contain a definition for HttpMode. If I take HttpMethod (which exists), I get the same behaviour as before.
Sparhawk
Have you noticed the new{} before the AjaxOptions?
Gregoire
No, I didn't.Now it's:using (Ajax.BeginForm("SendMessage", "MainChat", new {}, new AjaxOptions { UpdateTargetId = "test", HttpMethod = "POST" }))Your "new{}" must be the routeValues. Why should that make a difference?My action does have the HttpVerbs.Post attribute value and it is executed (the breakpoint is hit). So the "post" seems to be ok.
Sparhawk
I am using MVC2. Overload 5/11 is: actionName, controllerName, ajaxOptions. And it worked, too. Sorry, you are sending me the wrong way.
Sparhawk
Sorry I have notice this overload :(
Gregoire
+1  A: 

Make sure you have included the necessary script libraries:

<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftAjax.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcAjax.js") %>"></script>
Darin Dimitrov
Oops, I am sorry, that was really a newbie question. Including the scripts fixed it!
Sparhawk