i have a user 开发者_如何学运维control contains 3 textboxes and a button added to a aspx page . When clicked it should make a ajax call to controller and pass data of textboxes and it should return a message....how can i do this
Example:
<input type="text" name="foo" id="foo" />
<input type="text" name="bar" id="bar" />
<input type="text" name="baz" id="baz" />
<%= Html.ActionLink("send to controller", "Ajax", "Home", null, new { id = "ajaxLink" }) %>
and in a separate javascript file you could ajaxify this link:
$(function() {
$('#ajaxLink').click(function() {
var data = {
foo: $('#foo').val(),
bar: $('#bar').val(),
baz: $('#baz').val()
};
$.getJSON(this.href, data, function(result) {
alert(result.message);
});
return false;
});
});
and the corresponding controller action which will receive the call and return JSON:
public class HomeController: Controller
{
public ActionResult Ajax(string foo, string bar, string baz)
{
// TODO: do something with the arguments
return Json(
new { message = "Thanks for sending info" },
JsonRequestBehavior.AllowGet
);
}
}
EDIT: Removed the "new" keyword when assigning the value for var data
.
精彩评论