【问题标题】:mvc model validation required not working on all fields需要的 mvc 模型验证不适用于所有字段
【发布时间】:2012-09-03 18:30:11
【问题描述】:

我正在使用 ASP.NET MVC 4,但我遇到的问题是我的模型验证无法正常工作。出于某种原因,我的必填字段并非都必须填写。

这是我的模型:

public class MovieModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
        [Required]
        public string Genre { get; set; }
        [Required]
        public decimal Price { get; set; }

        public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
    }

这是视图:

@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>
                <label>Name:</label></td>
            <td>@Html.EditorFor(m => m.Name)</td>
            <td>@Html.ValidationMessageFor(m => m.Name)</td>
        </tr>
        <tr>
            <td>
                <label>Genre:</label></td>
            <td>@Html.EditorFor(m => m.Genre)</td>
            <td>@Html.ValidationMessageFor(m => m.Genre)</td>
        </tr>
        <tr>
            <td>
                <label>Price:</label></td>
            <td>@Html.EditorFor(m => m.Price)</td>
            <td>@Html.ValidationMessageFor(m => m.Price)</td>
        </tr>
    </table>
    <button type="submit">Submit</button>
}

这是我的行动:

[HttpPost]
        public ActionResult Add(MovieModel model)
        {
            if(ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View();
        }

现在事情是这样的:只要我只输入一个价格,modelstate.isvalid 就会变为 true。当悬停在我的模型上时,它说名称和流派都是空的。当然,它们是必需的,但验证不起作用。 此外,validationmessagefor 仅适用于价格。

我希望我没有忽略一些太荒谬的事情。感谢您的帮助!

【问题讨论】:

  • 点击提交时会发生什么?
  • 如果我填写价格,modelstate 变为有效,然后我去索引。如果我没有填写价格,我去 return View();只有价格给出了错误。名称和类型绝不会出错或使模型状态无效。

标签: asp.net-mvc


【解决方案1】:

将无效模型返回视图:

[HttpPost]
public ActionResult Add(MovieModel model)
{
    if(ModelState.IsValid)
    {
        return RedirectToAction("Index");
    }
    return View(model); // <----
}

哦,请确保所需的属性不允许空字符串

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.allowemptystrings.aspx

public class MovieModel
{
    public int Id { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Name { get; set; }

    public DateTime ReleaseDate { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Genre { get; set; }

    [Required]
    public decimal Price { get; set; }

    public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
}

【讨论】:

  • 感谢您的回答。然而这并没有奏效。只有价格会像往常一样被检查。
  • 无法解析符号 AllowEmptyStrings 是我目前遇到的错误。我的 Resharper 没有向我指出任何遗漏的参考资料,还是他忽略了什么?
  • @ThomasSchellekens 什么版本的 MVC?如果是 1 或 2,您可能需要为此编写自定义验证属性。
  • @ThomasSchellekens 它应该在那里,它是 .net 运行时的一部分,而不是特定于 MVC。您是否包含System.ComponentModel.DataAnnotations
  • 不包括在内。我现在已经包含了它。显然我的 resharper 添加了 microshoft.build.framework,它也支持 [required],但与数据注释冲突。谢谢你在这里的帮助,你拯救了我的一天。它按现在应该的方式工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
相关资源
最近更新 更多