如何修复MVC应用中的System.ArgumentException参数name为空错误
问题:ASP.NET MVC 创建候选者时出现 System.ArgumentException 错误
我正在做一个暑期项目,要开发一个能浏览和筛选候选者的作品集Web应用。已经用SQL数据库存储候选者信息和0-5分的编程语言熟练度评分,搭建MVC应用后,尝试创建新候选者时弹出错误:System.ArgumentException: 'Value cannot be null or empty. Parameter name: name'。我之前没接触过这个开发栈,不知道怎么解决,以下是我的视图代码:
@{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Candidate</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.First_Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.First_Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.First_Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Last_Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Last_Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Last_Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Date_of_birth, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Date_of_birth, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Date_of_birth, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.C__NET___ASP, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.C__NET___ASP, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.C__NET___ASP, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.CSS, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.CSS, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CSS, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.HTML, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.HTML, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.HTML, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Java, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Java, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Java, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.JavaScript, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.JavaScript, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.JavaScript, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Python, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Python, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Python, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Python_Flask, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Python_Flask, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Python_Flask, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
修复方案
1. 移除Id输入字段(最可能的原因)
通常Id是数据库自增主键,不需要用户手动输入。如果你的Candidate模型中Id配置为自增,直接从视图中删除Id的输入区域:
<!-- 删除这段代码 --> <div class="form-group"> @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" }) </div> </div>
同时确保模型中Id的特性配置正确:
public class Candidate { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } // 其他字段... }
2. 检查特殊字段命名
视图中的C__NET___ASP字段名是数据库转义后的结果(原字段名可能是C#/.NET/ASP),检查模型类中该字段的定义,确保没有空值绑定问题,比如是否添加了正确的特性(如[Range(0,5)])。
3. 验证控制器Create方法
确保POST方法正确接收模型参数:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Candidate candidate) { if (ModelState.IsValid) { db.Candidates.Add(candidate); db.SaveChanges(); return RedirectToAction("Index"); } return View(candidate); }
项目优化思路
- 拆分模型结构:把编程语言熟练度单独做成
Skill模型,和Candidate建立一对多关系,后续添加新语言无需修改主表。 - 无刷新筛选:用AJAX实现实时筛选,根据语言评分、地区等条件动态更新候选者列表。
- 增强表单验证:前端添加实时格式校验(邮箱、日期),后端给模型字段添加
[Required]、[EmailAddress]、[Range(0,5)]等特性。 - 分页加载:当候选者数量较多时,实现分页功能,避免页面加载卡顿。
内容的提问来源于stack exchange,提问作者Jamie_Talbot9
相关产品推荐
相关产品推荐

