ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例(2)

<link href="https://www.jb51.net/~/content/libs/bootstrap/css/bootstrap.min.css" type="text/css" /> <link href="https://www.jb51.net/~/content/libs/fa/css/font-awesome.min.css" type="text/css" /> <link href="https://www.jb51.net/~/content/app/css/site.css" type="text/css" /> <link href="https://www.jb51.net/~/content/app/css/bootstrap-extend.css" type="text/css" /> <link href="https://www.jb51.net/~/content/app/css/bootstrap-override.css" type="text/css" /> <script src="https://www.jb51.net/~/content/libs/jquery/jquery.min.js"></script> <script src="https://www.jb51.net/~/content/libs/jquery/jquery.validate-vsdoc.js"></script> <script src="https://www.jb51.net/~/content/libs/jquery/jquery.validate.js"></script> <script src="https://www.jb51.net/~/content/libs/jquery/jquery.validate.unobtrusive.js"></script> <script src="https://www.jb51.net/~/Content/libs/jquery/jquery.unobtrusive-ajax.min.js"></script>

后端代码————

[Route("~/DesignDeliverable/Edit/{id?}")] [HttpGet] public ActionResult Edit(Guid? id) { using ( EmpsDbContext db = new EmpsDbContext() ) { DesignDeliverable entity = null; if ( id.HasValue ) { entity = db.DesignDeliverables.SingleOrDefault(x => x.Id == id.Value); } else { entity = new DesignDeliverable(); } ViewBag.Rooms = RoomFacade.GetAll().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList(); ViewBag.Areas = AreaFacade.GetAll().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList(); return View(entity); } } [Route("~/DesignDeliverable/Save")] [HttpPost] public JsonResult Edit(DesignDeliverable model) { using ( EmpsDbContext db = new EmpsDbContext() ) { if ( !ModelState.IsValid ) { return Error(); } try { model.GroupId = new Guid("e030c300-849c-4def-807e-a5b35cf144a8"); //todo: fix this hardcode db.DesignDeliverables.AddOrUpdate(model); db.SaveChanges(); return Success(); } catch { //TODO: to store the exception message } return Error(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsppy.html