Ajax 在特定引导向导步骤上提交表单数据
我用几个步骤创建了一个引导向导。当用户单击向导第二步上的“保存并继续”按钮时,我希望在移动到第三步之前将输入的表单数据提交到基础数据存储。我似乎无法让表单提交工作,控制器上的actionmethod没有被调用。这是页面上的javascript,它正在被命中,但控制器操作不是Ajax 在特定引导向导步骤上提交表单数据,ajax,asp.net-mvc,twitter-bootstrap-3,wizard,Ajax,Asp.net Mvc,Twitter Bootstrap 3,Wizard,我用几个步骤创建了一个引导向导。当用户单击向导第二步上的“保存并继续”按钮时,我希望在移动到第三步之前将输入的表单数据提交到基础数据存储。我似乎无法让表单提交工作,控制器上的actionmethod没有被调用。这是页面上的javascript,它正在被命中,但控制器操作不是 $(“#scheduleReport”)。在(“单击”,函数(){ //通过属性获取记录的ID //var id=$(this.attr('data-id'); $('#frmAddSchedule').validate(
$(“#scheduleReport”)。在(“单击”,函数(){
//通过属性获取记录的ID
//var id=$(this.attr('data-id');
$('#frmAddSchedule').validate();
$('#frmAddSchedule')。提交();
});
$('frmAddSchedule')。在('submit',函数(e)上{
变量$form=$(e.target);
如果($form.valid()){
e、 预防默认值();
$.ajax({
url:'@url.Action(“创建”、“报告调度器”),
数据:$form.serialize(),
async:true,
键入:“POST”,
成功:函数(returnval){
if(returnval.success==true){
$(“#schedulerGrid”).igGrid(“数据绑定”);
}
if(returnval.success==false){
//$form.parents('.bootbox').modal('hide');
警报({title:'ERROR',消息:returnval['responseText']});
}
},
错误:函数(returnval){
//$form.parents('.bootbox').modal('hide');
警报(returnval['responseText']);
}
});
}
});代码>好吧,作为一个开始,我可能可以帮助您完成调用控制器操作的部分。关于“最佳做法”的问题,你提供了一些细节
因此:
或
没有好处:
- 我使用了
数据类型:json
。您还可以使用数据类型:html
。如果,那么
回显不带json\u编码的字符串或数组
- 我将您的
returnval
重命名为response
- 观察ajax的参数
fail()
,例如旧的error()
- 我在函数中构造了js代码
- 由于我来自PHP世界,我使用
echo
打印和json\u编码
以json格式编码
祝你好运 我最终发现了isue,我使用的向导示例中已经有一个表单元素(没有注意到),我将其包装在自己的表单中,该表单正在停止表单提交。它现在可以正常工作了我尝试了你的代码,但没有成功,控制器操作仍然没有被命中(mvc&c),你说的异步是什么意思:真的,//请不要使用它!:-)使用“async:false”时,每次发送ajax请求时,浏览器都会冻结,等待它完成。根据定义,AJAX是一种异步调用,应该保持这种状态。@proteus您确定url:'@url.Action(“Create”,“ReportScheduler”)'
正确吗?
/**
* On document ready.
*
* @return void
*/
$(document).ready(function () {
activateScheduleReport();
activateAddScheduleFormSubmit();
});
/**
* Activate schedule report click event.
*
* @return void
*/
function activateScheduleReport() {
$("#scheduleReport").on("click", function () {
// Get the record's ID via attribute
//var id = $(this).attr('data-id');
validateAddScheduleForm();
submitAddScheduleForm();
});
}
/**
* Activate add schedule form submit.
*
* @return void
*/
function activateAddScheduleFormSubmit() {
$('#frmAddSchedule').submit(function (e) {
var $form = $(this);
var ajax = $.ajax({
async: true, // Please don't use this! :-)
method: 'post',
dataType: 'json',
url: '@Url.Action("Create", "ReportScheduler")',
data: $form.serialize()
});
ajax.done(function (response, textStatus, jqXHR) {
if (response.success) {
$("#schedulerGrid").igGrid("dataBind");
} else {
//$form.parents('.bootbox').modal('hide');
bootbox.alert({
title: '<div class="text-center text-danger"><i class="fa fa-exclamation-triangle"></i> ERROR</div>',
message: response.responseText
});
}
});
ajax.fail(function (jqXHR, textStatus, errorThrown) {
//$form.parents('.bootbox').modal('hide');
bootbox.alert(textStatus + ' ' + errorThrown);
});
ajax.always(function (response, textStatus, jqXHR) {
// Do whatever you want here...
});
// Use this to prevent page refreshing. No need for preventDefault().
return false;
});
}
/**
* Validate add schedule form.
*
* @return void
*/
function validateAddScheduleForm() {
$('#frmAddSchedule').validate();
}
/**
* Submit add schedule form.
*
* @return void
*/
function submitAddScheduleForm() {
$('#frmAddSchedule').submit();
}
echo json_encode('whatever response string');
echo json_encode(array(responseText: 'whatever response string'));