使用ViewModel提交POST创建游戏事件失败求助
游戏事件创建页面提交失败排查方案
我正在开发一个可创建游戏事件的网站,原本创建新事件时直接输入GameID,现在为了让用户选择游戏名称而非输入ID,改用ViewModel实现。页面的游戏下拉列表显示正常,但点击提交按钮后仅刷新页面,数据库无新增数据,POST请求返回200状态码且无报错。不使用ViewModel时创建功能正常。
ViewModel及实体类定义
public class EventCreateVM { public Events Event { get; set; } public IEnumerable<SelectListItem> Games { get; set; } }
public class Events { public int EventID { get; set; } public int GameID { get; set; } [DisplayName("Event Name")] public string EventName { get; set; } public string Server { get; set; } [DisplayName("Max Players")] public int MaxPlayers { get; set; } public string Type { get; set; } public string Platform { get; set; } public string Description { get; set; } [DisplayName("Event Time")] public DateTime DateTime { get; set; } }
视图代码(适配ViewModel)
@model AgileTeamFour.UI.ViewModels.EventCreateVM @{ ViewData["Title"] = "Create"; } <h1>Create</h1> <h4>Events</h4> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create" asp-controller="Event"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> @* <div class="form-group"> <label asp-for="Event.EventID" class="control-label"></label> <input asp-for="Event.EventID" class="form-control" /> <span asp-validation-for="Event.EventID" class="text-danger"></span> </div> *@ <div class="form-group"> <label asp-for="Event.GameID" class="control-label"></label> <select asp-for="Event.GameID" class="form-control" asp-items="Model.Games"></select> <span asp-validation-for="Event.GameID" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.EventName" class="control-label"></label> <input asp-for="Event.EventName" class="form-control" /> <span asp-validation-for="Event.EventName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.Server" class="control-label"></label> <input asp-for="Event.Server" class="form-control" /> <span asp-validation-for="Event.Server" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.MaxPlayers" class="control-label"></label> <input asp-for="Event.MaxPlayers" class="form-control" /> <span asp-validation-for="Event.MaxPlayers" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.Type" class="control-label"></label> <input asp-for="Event.Type" class="form-control" /> <span asp-validation-for="Event.Type" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.Platform" class="control-label"></label> <input asp-for="Event.Platform" class="form-control" /> <span asp-validation-for="Event.Platform" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.Description" class="control-label"></label> <input asp-for="Event.Description" class="form-control" /> <span asp-validation-for="Event.Description" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Event.DateTime" class="control-label"></label> <input asp-for="Event.DateTime" class="form-control" /> <span asp-validation-for="Event.DateTime" 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");} }
控制器代码
public ActionResult Create() { var games = GameManager.Load(); var viewModel = new EventCreateVM { Event = new Events(), Games = games.Select(g => new SelectListItem { Value = g.GameID.ToString(), Text = g.GameName }).ToList() }; return View(viewModel); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(EventCreateVM viewmodel) { try { if (ModelState.IsValid) { int eventID = 0; EventManager.Insert(ref eventID, viewmodel.Event.GameID, viewmodel.Event.EventName, viewmodel.Event.Server, viewmodel.Event.MaxPlayers, viewmodel.Event.Type, viewmodel.Event.Platform, viewmodel.Event.Description, viewmodel.Event.DateTime); return RedirectToAction(nameof(Index)); } return View(viewmodel); } catch { return View(); } }
排查建议
检查模型验证细节:在POST方法开头添加调试代码,查看所有模型验证错误:
var errors = ModelState.Values.SelectMany(v => v.Errors);asp-validation-summary="ModelOnly"仅显示模型级错误,字段级验证失败可能被隐藏,导致ModelState.IsValid为false但页面无提示。重新填充下拉列表数据源:当模型验证失败返回视图时,
viewmodel.Games会丢失数据,需重新加载并赋值:if (!ModelState.IsValid) { var games = GameManager.Load(); viewmodel.Games = games.Select(g => new SelectListItem { Value = g.GameID.ToString(), Text = g.GameName }).ToList(); return View(viewmodel); }验证数据传递完整性:用浏览器开发者工具查看POST请求的Form Data,确认所有字段(尤其是
Event.GameID、Event.EventName等)是否正确发送到服务器。修复异常处理逻辑:当前catch块直接返回空视图,丢失错误信息,改为记录异常并返回带错误提示的视图:
catch (Exception ex) { ModelState.AddModelError("", $"创建失败:{ex.Message}"); var games = GameManager.Load(); viewmodel.Games = games.Select(g => new SelectListItem { Value = g.GameID.ToString(), Text = g.GameName }).ToList(); return View(viewmodel); }检查
EventManager.Insert方法:确认该方法是否正确处理参数,是否有内部吞掉的异常,可在方法内添加日志或直接调试参数值是否符合数据库要求。
内容的提问来源于stack exchange,提问作者Jimmy Dictus
相关产品推荐
相关产品推荐

