我想通过单击访问我的 Controller 。该表单是为另一个函数设置的回发。如果有表格我知道如何发布。有没有一种方法可以在不使用表单的情况下访问 Controller ,如果是的话我该怎么做?
<% using (Html.BeginForm("OneMethod", "Home", FormMethod.Post, new { id = "formNext" })) {
<input id="addContent" type="button" onclick="addContent(ind1, ind2)"/>
<% } %>
jquery
function(addContent(ind1, ind2) {
//post should go to "TwoMethod" and not "OneMethod" but not sure how...
$.post($(this).attr("action"), $(this).serialize(), function(response) {
//code
}, 'json');
}
最佳答案
这是我编写的一个很好的函数,用于从 AJAX 发布它。
submitForm("home/twomethod", $("#formNext"),function(e){do stuff});
哪个调用:
function sumbitForm(url, DetailsForm, SuccessCallBack)
{
$.ajax({
url: url,
type: "POST",
contentType: "application/x-www-form-urlencoded",
dataType: "json",
data: $(DetailsForm).serialize(),
success: SuccessCallBack,
error: MyNamespace.handleAjaxError
});
}
关于jquery - 如何将 $.post 与按钮 onclick 功能而不是表单一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875496/