Javascript 无法重载Ajax.BeginForm的提交方法
我试图从razor视图提交一个ajax表单,我希望控制器返回一个JSON对象。当我使用(“#form0”).submit(提醒(“hi”);数据进入控制器,我得到一个警报。但是,当我使用(“#form0”).submit(函数(){alert(“hi”);});数据没有传递,我也没有收到警报。我觉得这是我语法中的一个小错误,我没有。以下是相关代码: jquery:Javascript 无法重载Ajax.BeginForm的提交方法,javascript,jquery,ajax,asp.net-mvc-3,razor,Javascript,Jquery,Ajax,Asp.net Mvc 3,Razor,我试图从razor视图提交一个ajax表单,我希望控制器返回一个JSON对象。当我使用(“#form0”).submit(提醒(“hi”);数据进入控制器,我得到一个警报。但是,当我使用(“#form0”).submit(函数(){alert(“hi”);});数据没有传递,我也没有收到警报。我觉得这是我语法中的一个小错误,我没有。以下是相关代码: jquery: $(function () { //setting up the schedule modal dialoag. $("#sched
$(function () {
//setting up the schedule modal dialoag.
$("#schedModal").dialog({
buttons: {
Submit:
function () {
$("#form0").ajaxSubmit(function () {
//this is where I want to put the magic, but I need the alert to fire first.
alert("hi");
return false;
});
},
Cancel:
function () {
$(this).dialog("close");
}
},
autoOpen: false,
minHeight: 350,
modal: true,
resizable: false
});
目标视图:
@model FSDS.DataModels.Schedule
@using (Ajax.BeginForm("scheduleNew", null, new AjaxOptions { UpdateTargetId = "partial" }, new {}))
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.ScheduleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ScheduleName)
@Html.ValidationMessageFor(model => model.ScheduleName)
</div>
@* tons of other labels and editor fields go in here, omitted for brevity. *@
}
只需使用$('#form0').submit()代码>:
然后在AjaxForm中定义一个OnSuccess
处理程序,当AJAX请求成功时将调用该处理程序:
@using (Ajax.BeginForm("scheduleNew", null, new AjaxOptions { OnSuccess = "success", UpdateTargetId = "partial" }, new {}))
最后是success
javascript处理程序:
function success(data) {
// the form was successfully submitted using an AJAX call.
// here you could test whether the data parameter
// represents a JSON object or a partial view
if (data.ScheduleName) {
// the controller action returned the schedule JSON object
// => act accordingly
} else {
// the controller action returned a partial view
// => act accordingly
}
}
像做梦一样工作。你是一位绅士和学者,达林·迪米特洛夫。
@using (Ajax.BeginForm("scheduleNew", null, new AjaxOptions { OnSuccess = "success", UpdateTargetId = "partial" }, new {}))
function success(data) {
// the form was successfully submitted using an AJAX call.
// here you could test whether the data parameter
// represents a JSON object or a partial view
if (data.ScheduleName) {
// the controller action returned the schedule JSON object
// => act accordingly
} else {
// the controller action returned a partial view
// => act accordingly
}
}