ASP.NET MVC中Create控制器Action未触发问题求助
问题描述
使用Entity Framework创建基础MVC解决方案,在Create表单输入测试数据并点击Create按钮后无任何反应:数据库未新增条目,页面也未返回控制器Index页。大部分代码由Visual Studio自动生成,不清楚问题所在。
预期控制器应处理Create动作,获取表单数据存入对应数据库后返回Index页,但实际页面停留在Create表单,测试数据仍保留,数据库无新增记录。
Create视图代码
@model DCServer.Models.Driver @{ ViewData["Title"] = "Create"; } <h1>Create</h1> <h4>Driver</h4> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="scooberId" class="control-label"></label> <input asp-for="scooberId" class="form-control" /> <span asp-validation-for="scooberId" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="driverName" class="control-label"></label> <input asp-for="driverName" class="form-control" /> <span asp-validation-for="driverName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="driverScore" class="control-label"></label> <input asp-for="driverScore" class="form-control" /> <span asp-validation-for="driverScore" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="employedTime" class="control-label"></label> <input asp-for="employedTime" class="form-control" /> <span asp-validation-for="employedTime" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="amtApproaches" class="control-label"></label> <input asp-for="amtApproaches" class="form-control" /> <span asp-validation-for="amtApproaches" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="amtEvaluated" class="control-label"></label> <input asp-for="amtEvaluated" class="form-control" /> <span asp-validation-for="amtEvaluated" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="lastEvaluated" class="control-label"></label> <input asp-for="lastEvaluated" class="form-control" /> <span asp-validation-for="lastEvaluated" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </form> </div> </div> <div> <a asp-action="Index">Back to List</a> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Driver模型代码
using MessagePack; using System; using System.Collections.Generic; namespace DCServer.Models { public class Driver { public int Id { get; set; } public int scooberId { get; set; } public string driverName { get; set; } public string driverScore { get; set; } public float employedTime { get; set; } public string amtApproaches { get; set; } public virtual ICollection<Approach> Approaches { get; set; } public int amtEvaluated { get; set; } public float lastEvaluated { get; set; } public virtual ICollection<Evaluation> Evaluations { get; set; } public Driver() { } } }
Drivers控制器代码
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using DCServer.Data; using DCServer.Models; namespace DCServer.Controllers { public class DriversController : Controller { private readonly ApplicationDbContext _context; public DriversController(ApplicationDbContext context) { _context = context; } // GET: Drivers public async Task<IActionResult> Index() { return View(await _context.Driver.ToListAsync()); } // GET: Drivers/Details/5 public async Task<IActionResult> Details(int? id) { if (id == null || _context.Driver == null) { return NotFound(); } var driver = await _context.Driver .FirstOrDefaultAsync(m => m.Id == id); if (driver == null) { return NotFound(); } return View(driver); } // GET: Drivers/Create public IActionResult Create() { return View(); } // POST: Drivers/Create // To protect from overposting attacks, enable the specific properties you want to bind to. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("Id,scooberId,driverName,driverScore,employedTime,amtApproaches,amtEvaluated,lastEvaluated")] Driver driver) { try { if (ModelState.IsValid) { _context.Add(driver); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } } catch (DataException /* dex */) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(driver); } // GET: Drivers/Edit/5 public async Task<IActionResult> Edit(int? id) { if (id == null || _context.Driver == null) { return NotFound(); } var driver = await _context.Driver.FindAsync(id); if (driver == null) { return NotFound(); } return View(driver); } // POST: Drivers/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("Id,scooberId,driverName,driverScore,employedTime,amtApproaches,amtEvaluated,lastEvaluated")] Driver driver) { if (id != driver.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(driver); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DriverExists(driver.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(driver); } // GET: Drivers/Delete/5 public async Task<IActionResult> Delete(int? id) { if (id == null || _context.Driver == null) { return NotFound(); } var driver = await _context.Driver .FirstOrDefaultAsync(m => m.Id == id); if (driver == null) { return NotFound(); } return View(driver); } // POST: Drivers/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<IActionResult> DeleteConfirmed(int id) { if (_context.Driver == null) { return Problem("Entity set 'ApplicationDbContext.Driver' is null."); } var driver = await _context.Driver.FindAsync(id); if (driver != null) { _context.Driver.Remove(driver); } await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool DriverExists(int id) { return _context.Driver.Any(e => e.Id == id); } } }
内容的提问来源于stack exchange,提问作者Bram
相关产品推荐
相关产品推荐

