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
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
.