You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# & ASP.NET MVC问题:输入数据无法传递到控制器Action

解决ASP.NET MVC中ModelState.IsValid始终为false的问题

问题根源

你的代码存在三个核心问题导致验证失败:

  1. 模型绑定前缀不匹配:视图表单字段名称带NewArticle前缀(比如NewArticle.Title),但Action接收的是ArticleViewModel参数,默认绑定器无法识别前缀,导致字段值无法正确映射到模型,触发必填验证。
  2. TagIds的null验证问题:List<int>是引用类型,用户未选标签时TagIds会被绑定为null,[Required]属性会判定验证失败。
  3. 视图未显示验证错误:没有添加验证消息显示代码,你无法直接看到具体的验证失败原因。

解决方案及修改代码

1. 修正模型绑定前缀

在Action的参数上添加[Bind(Prefix = "NewArticle")],告诉绑定器要识别NewArticle前缀的字段:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddArticle([Bind(Prefix = "NewArticle")] ArticleViewModel model)
{
    // 原有逻辑不变
}

2. 修复TagIds的验证规则

将ArticleViewModel中TagIds的[Required]替换为[MinLength(1)],确保用户至少选择一个标签,同时在构造函数初始化空列表避免null问题:

public class ArticleViewModel
{
    [Required(ErrorMessage = "标题为必填项。")]
    public string Title { get; set; }

    [Required(ErrorMessage = "内容为必填项。")]
    public string Content { get; set; }

    [Required(ErrorMessage = "简短描述为必填项。")]
    public string ShortDescription { get; set; }

    [MinLength(1, ErrorMessage = "至少选择一个标签。")]
    public List<int> TagIds { get; set; }

    // 初始化空列表,避免未选标签时为null
    public ArticleViewModel()
    {
        TagIds = new List<int>();
    }
}

3. 在视图中添加验证错误提示

给每个表单字段添加验证消息显示,方便用户看到错误:

@model BlogApplication.Models.DashboardViewModel
....
<h3>Add New Article</h3>

@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(m => m.NewArticle.Title, "Title")
        @Html.TextBoxFor(m => m.NewArticle.Title, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.NewArticle.Title, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewArticle.ShortDescription, "Short Description")
        @Html.TextBoxFor(m => m.NewArticle.ShortDescription, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.NewArticle.ShortDescription, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewArticle.Content, "Content")
        @Html.TextAreaFor(m => m.NewArticle.Content, new { @class = "form-control", rows = 5 })
        @Html.ValidationMessageFor(m => m.NewArticle.Content, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewArticle.TagIds, "Tags")
        @Html.ListBoxFor(m => m.NewArticle.TagIds, new MultiSelectList(Model.Tags, "TagId", "TagName"), new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.NewArticle.TagIds, "", new { @class = "text-danger" })
    </div>
    <button type="submit" class="btn btn-primary">Add Article</button>
}

额外调试技巧

如果还是有问题,可以在Action中打印所有验证错误的键和消息,精准定位:

foreach (var key in ModelState.Keys)
{
    var errors = ModelState[key].Errors;
    foreach (var error in errors)
    {
        System.Diagnostics.Debug.WriteLine($"字段: {key}, 错误: {error.ErrorMessage}");
    }
}

内容的提问来源于stack exchange,提问作者Itay Nafrin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 06:13:12