MVC AJAX
March 17, 2010 Leave a comment
I started working with MVC Ajax last week. I started working though the examples found the Wrox’s Pro ASP.NET MVC
<% using (Html.BeginForm(new { action = "HelloAjax" }))
{ %>
<input type="text" name="query" size="40" />
<input type="submit" value="go" />
<%} %>
<div id="results">
</div>
And I got this
It took about a second to understand that I didn’t have a “HelloAjax” method so I went farther into the chapter to find this:
public string HelloAjax(string query)
{
return "You entered: " + query;
}
That works better:
I then changed the form to AJAXify it:
<%using (Ajax.BeginForm("HelloAjax",
new AjaxOptions { UpdateTargetId = "results" }))
{%>
<%=Html.TextBox("query", null, new { size = 40 })%>
<input type="submit" />
<%} %>
<div id="results">
And got it:
Which is what I expected. I then loaded Fiddler to see the traffic
The important thing with Fiddler is that when you are using your local machine, you need to either type "localhost." (note the extra period) or the machine name. You also need to allow Fiddler to be a proxy so you need to adjust Vista’s security settings.
I am really impressed how light weight the payload is.