gpt4 book ai didi

c# - 将 required 添加到 LabelFor

转载 作者:行者123 更新时间:2023-11-30 12:24:19 25 4
gpt4 key购买 nike

我有以下代码:

@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })

我正在寻找一种方法来向它添加 html 属性 required ,这样用户就无法在没有填写字段的情况下提交。但是现在确定怎么办?我知道最简单的方法是添加 required 但不知道如何,我尝试使用 @html "reguired" 没有任何运气。

编辑:Answere = required = ""

最佳答案

您可以添加 RequiredAttribute到您的模型属性:

[Required(ErrorMessage = "Title is required")]
public string Title { get;set; }

并将 ValidationMessageFor 添加到您的 cshtml:

@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.Model)

然后将模型验证添加到 Controller 方法via .它是 asp.net mvc 的标准管道。

您还可以实现 your own HtmlHepler将必需的属性添加到您的 html 代码中。

关于c# - 将 required 添加到 LabelFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com