本文介绍了保存方法的ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想对保存方法做一个javascript请求,如下所示: [AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null ; if (ModelState.IsValid) { ReservationModel r; Mapper.CreateMap< UpdateAgendaEventViewModel,ReservationModel>() .ForMember(d = > d.ReservationId,opt = > opt.MapFrom(s = > s.id)) .ForMember(d = > d.FuneralId,opt = > opt.MapFrom(s = > s.funeralId)) .ForMember(d = > d.RoomId,opt = > opt.MapFrom(s = > s.roomId)) .ForMember(d = > d.Begin,opt = > opt.MapFrom(s = > ; s.start)) .ForMember(d = > d.End,opt = &克t; opt.MapFrom(s = > s.end)) .ForMember(d = > d.RoomItems,opt = > opt.MapFrom(s = > s.RoomItems)); if (!string.IsNullOrEmpty(viewModel.id)) {r = new ReservationModel( int .Parse(viewModel.id)); r.Update(Mapper.Map< UpdateAgendaEventViewModel,ReservationModel>(viewModel)); } else { r = Mapper.Map< UpdateAgendaEventViewModel,ReservationModel>(viewModel); } Mapper.CreateMap< UpdateAgendaEventViewModel,ReservationModel>(); ReservationModel newInfo = Mapper.Map< UpdateAgendaEventViewModel,ReservationModel>(viewModel); reservation.Update(Mapper.Map< UpdateAgendaEventViewModel,ReservationModel>(viewModel)); reservation.Save(); } return Json( new {succes = true }); } 和 javascript: $( function (){ $ .ajax({ url:' / AgendaEvent / UpdateSave /', type:' POST', data:jsonData, datatype:' html', // data :JSON.stringify(dataResponse), // contentType:'application / json; charset = utf-8', // dataType:'json', 错误: function (响应){ alert( 错误); },成功:功能(数据){ console .log(数据)} }); }); 但我收到此错误: 未捕获ReferenceError:jsonData未定义 数据:jsonData, 谢谢 Oke, 我现在有这样的: // [AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null; if(ModelState.IsValid) { ReservationModel r; Mapper.CreateMap < UpdateAgendaEventViewModel, ReservationModel > () .ForMember(d => d.ReservationId ,opt => opt.MapFrom(s => s.id)) .ForMember(d => d.FuneralId,opt => opt.MapFrom(s => s.funeralId)) .ForMember(d => d.RoomId,opt => opt.MapFrom(s => s.roomId)) .ForMember(d => d.Begin,opt => ; opt.MapFrom(s => s.start)) .ForMember(d => d.End,opt => opt.MapFrom(s => s.end)) .ForMember(d => d.RoomItems,opt => opt.MapFrom(s => s.RoomItems)); if(!string.IsNullOrEmpty(viewModel.id)) { r = new ReservationModel(int.Parse(viewModel.id)); r.Update(Mapper.Map < UpdateAgendaEventViewModel, ReservationModel > (viewModel)); } else {r = Mapper.Map < UpdateAgendaEventViewModel , ReservationModel > (视图模型); } // Mapper.CreateMap < UpdateAgendaEventViewModel, ReservationModel > (); // ReservationModel newInfo = Mapper.Map < UpdateAgendaEventViewModel, ReservationModel > (viewModel); //reservation.Update(Mapper.Map< UpdateAgendaEventViewModel, ReservationModel > (viewModel)); // r.Save(); RoomModel room = new RoomModel(r.RoomId.Value); bool boolEditable =(!r.Definitive.HasValue)?真假; if(r.Save()) { 返回Json(新的AgendaEventModel() { className = boolEditable?:agenda-event inactive, id = r.ReservationId.ToString(), title = room.Name, start = string.Format ({0:yyyy-MM-ddTHH:mm:ss},r.Begin), end = string.Format({0:yyyy-MM-ddTHH:mm:ss},r。结束), color = room.Color, backgroundColor = room.BackgroundColor, borderColor = room.BorderColor, textColor = room.TextColor, editable = boolEditable, eventStartEditable = boolEditable, eventDurationEditable = boolEditable,}); } else { //this.ShowMessage(Message.Type.Danger,Resources.Default.SaveFailure,true); //this.ShowMessage(Message.Type.Danger,r.Error,true); this.ShowMessage(Message.Type.Danger,r.Error); } } 返回Json(new {succes = true}); } 和ajax电话: $( function (){ // var premisesViewModel = $('form')。serializeObject(); $ .ajax({ url:' / AgendaEvent / UpdateSave /', type:' POST', data:{}, datatype: JSON, // data:JSON.stringify (dataResponse), // contentType:'application / json; charset = utf-8', // dataType:'json', 错误: function (响应){ alert( 错误); },成功:功能(数据){ console .log(数据)} }); }); 但是模型的所有值都为null 解决方案 ( function (){ .ajax({ url:' / AgendaEvent / UpdateSave /', type:' POST', data:jsonData, datatype:' html', // data:JSON .stringify(dataResponse), // contentType:'application / json; charset = utf -8', // dataType:'json', 错误:功能 (响应){ alert( 错误); },成功:功能(数据){ console .log(数据)} }); }); 但我收到此错误: 未捕获ReferenceError:jsonData未定义 数据:jsonData, 谢谢 Oke, 我现在有这样的: // [AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null; if(ModelState.IsValid) { ReservationModel r; Mapper.CreateMap < UpdateAgendaEventViewModel, ReservationModel > () .ForMember(d => d.ReservationId ,opt => opt.MapFrom(s => s.id)) .ForMember(d => d.FuneralId,opt => opt.MapFrom(s => s.funeralId)) .ForMember(d => d.RoomId,opt => opt.MapFrom(s => s.roomId)) .ForMember(d => d.Begin,opt => ; opt.MapFrom(s => s.start)) .ForMember(d => d.End,opt => opt.MapFrom(s => s.end)) .ForMember(d => d.RoomItems,opt => opt.MapFrom(s => s.RoomItems)); if(!string.IsNullOrEmpty(viewModel.id)) { r = new ReservationModel(int.Parse(viewModel.id)); r.Update(Mapper.Map < UpdateAgendaEventViewModel, ReservationModel > (viewModel)); } else {r = Mapper.Map < UpdateAgendaEventViewModel , ReservationModel > (视图模型); } // Mapper.CreateMap < UpdateAgendaEventViewModel, ReservationModel > (); // ReservationModel newInfo = Mapper.Map < UpdateAgendaEventViewModel, ReservationModel > (viewModel); //reservation.Update(Mapper.Map< UpdateAgendaEventViewModel, ReservationModel > (viewModel)); // r.Save(); RoomModel room = new RoomModel(r.RoomId.Value); bool boolEditable =(!r.Definitive.HasValue)?真假; if(r.Save()) { 返回Json(新的AgendaEventModel() { className = boolEditable?:agenda-event inactive, id = r.ReservationId.ToString(), title = room.Name, start = string.Format ({0:yyyy-MM-ddTHH:mm:ss},r.Begin), end = string.Format({0:yyyy-MM-ddTHH:mm:ss},r。结束), color = room.Color, backgroundColor = room.BackgroundColor, borderColor = room.BorderColor, textColor = room.TextColor, editable = boolEditable, eventStartEditable = boolEditable, eventDurationEditable = boolEditable,}); } else { //this.ShowMessage(Message.Type.Danger,Resources.Default.SaveFailure,true); //this.ShowMessage(Message.Type.Danger,r.Error,true); this.ShowMessage(Message.Type.Danger,r.Error); } } 返回Json(new {succes = true}); } 和ajax电话: ( function (){ // var premisesViewModel = Hi ,I want to do a javascript request on the save method, like this:[AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null; if (ModelState.IsValid) { ReservationModel r; Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>() .ForMember(d => d.ReservationId, opt => opt.MapFrom(s => s.id)) .ForMember(d => d.FuneralId, opt => opt.MapFrom(s => s.funeralId)) .ForMember(d => d.RoomId, opt => opt.MapFrom(s => s.roomId)) .ForMember(d => d.Begin, opt => opt.MapFrom(s => s.start)) .ForMember(d => d.End, opt => opt.MapFrom(s => s.end)) .ForMember(d => d.RoomItems, opt => opt.MapFrom(s => s.RoomItems)); if (!string.IsNullOrEmpty(viewModel.id)) { r = new ReservationModel(int.Parse(viewModel.id)); r.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); } else { r = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); } Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>(); ReservationModel newInfo = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); reservation.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); reservation.Save(); } return Json(new { succes = true }); }and the javascript: $(function () { $.ajax({ url: '/AgendaEvent/UpdateSave/', type: 'POST', data: jsonData, datatype: 'html', // data: JSON.stringify(dataResponse), //contentType: 'application/json; charset=utf-8', //dataType: 'json', error: function (response) { alert("Error"); }, success: function (data) { console.log(data) } });});But I get this error:Uncaught ReferenceError: jsonData is not defined data: jsonData,Thank youOke, I have it now like this:// [AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null; if (ModelState.IsValid) { ReservationModel r; Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>() .ForMember(d => d.ReservationId, opt => opt.MapFrom(s => s.id)) .ForMember(d => d.FuneralId, opt => opt.MapFrom(s => s.funeralId)) .ForMember(d => d.RoomId, opt => opt.MapFrom(s => s.roomId)) .ForMember(d => d.Begin, opt => opt.MapFrom(s => s.start)) .ForMember(d => d.End, opt => opt.MapFrom(s => s.end)) .ForMember(d => d.RoomItems, opt => opt.MapFrom(s => s.RoomItems)); if (!string.IsNullOrEmpty(viewModel.id)) { r = new ReservationModel(int.Parse(viewModel.id)); r.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); } else { r = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); } // Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>(); // ReservationModel newInfo = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); //reservation.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); // r.Save(); RoomModel room = new RoomModel(r.RoomId.Value); bool boolEditable = (!r.Definitive.HasValue) ? true : false; if (r.Save()) { return Json(new AgendaEventModel() { className = boolEditable ? "" : "agenda-event inactive", id = r.ReservationId.ToString(), title = room.Name, start = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.Begin), end = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.End), color = room.Color, backgroundColor = room.BackgroundColor, borderColor = room.BorderColor, textColor = room.TextColor, editable = boolEditable, eventStartEditable = boolEditable, eventDurationEditable = boolEditable, }); } else { //this.ShowMessage(Message.Type.Danger, Resources.Default.SaveFailure, true); //this.ShowMessage(Message.Type.Danger, r.Error, true); this.ShowMessage(Message.Type.Danger, r.Error); } } return Json(new { succes = true }); }and the ajax call:$(function () { //var premisesViewModel = $('form').serializeObject(); $.ajax({ url: '/AgendaEvent/UpdateSave/', type: 'POST', data: {}, datatype: "JSON", // data: JSON.stringify(dataResponse), //contentType: 'application/json; charset=utf-8', //dataType: 'json', error: function (response) { alert("Error"); }, success: function (data) { console.log(data) } }); });But all the values of the model are null 解决方案 (function () {.ajax({ url: '/AgendaEvent/UpdateSave/', type: 'POST', data: jsonData, datatype: 'html', // data: JSON.stringify(dataResponse), //contentType: 'application/json; charset=utf-8', //dataType: 'json', error: function (response) { alert("Error"); }, success: function (data) { console.log(data) } });});But I get this error:Uncaught ReferenceError: jsonData is not defined data: jsonData,Thank youOke, I have it now like this:// [AuthorizeLogin] public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel) { ReservationModel reservation = null; if (ModelState.IsValid) { ReservationModel r; Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>() .ForMember(d => d.ReservationId, opt => opt.MapFrom(s => s.id)) .ForMember(d => d.FuneralId, opt => opt.MapFrom(s => s.funeralId)) .ForMember(d => d.RoomId, opt => opt.MapFrom(s => s.roomId)) .ForMember(d => d.Begin, opt => opt.MapFrom(s => s.start)) .ForMember(d => d.End, opt => opt.MapFrom(s => s.end)) .ForMember(d => d.RoomItems, opt => opt.MapFrom(s => s.RoomItems)); if (!string.IsNullOrEmpty(viewModel.id)) { r = new ReservationModel(int.Parse(viewModel.id)); r.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); } else { r = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); } // Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>(); // ReservationModel newInfo = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel); //reservation.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel)); // r.Save(); RoomModel room = new RoomModel(r.RoomId.Value); bool boolEditable = (!r.Definitive.HasValue) ? true : false; if (r.Save()) { return Json(new AgendaEventModel() { className = boolEditable ? "" : "agenda-event inactive", id = r.ReservationId.ToString(), title = room.Name, start = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.Begin), end = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.End), color = room.Color, backgroundColor = room.BackgroundColor, borderColor = room.BorderColor, textColor = room.TextColor, editable = boolEditable, eventStartEditable = boolEditable, eventDurationEditable = boolEditable, }); } else { //this.ShowMessage(Message.Type.Danger, Resources.Default.SaveFailure, true); //this.ShowMessage(Message.Type.Danger, r.Error, true); this.ShowMessage(Message.Type.Danger, r.Error); } } return Json(new { succes = true }); }and the ajax call:(function () { //var premisesViewModel = 这篇关于保存方法的ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 09:31