MVC 5 文本框对于 data_val_required、data_val
MVC 5 textboxFor data_val_required, data_val
我的视图中有以下文本框
@Html.TextBoxFor(m => m.AdvertModel.Title, new
{
@class = "form-control",
type = "text",
placeholder = "Enter a descriptive title about the item for sale",
id = "Enter a descriptive title",
data_val_required = "The Title field is required.",
data_val = "true"
})
你可以看到我已经添加了 data_val_required 和 data_val 属性来完成它,呈现如下:
<input id="Enter a descriptive title" class="form-control" type="text" value="" placeholder="Enter a descriptive title about the item for sale" name="Title" data-val-required="The Title field is required." data-val-maxlength-max="100" data-val-maxlength="The field Title must be a string or array type with a maximum length of '100'." data-val="true">
当我 运行 应用程序和 this 并将其留空并单击提交时 ModelState.isValid 始终为真,当我期望它为假时,为什么它一直说它为真?
当您将表单提交给 POST 方法时,表单值包含 key/value 对,由每个控件的 name
属性和 value
属性组成。在您的情况下,它将是 AdvertModel.Title=The value entered in the textbox
。没有关于控件中其他属性的信息被发送到服务器。
data-*
属性由 html 助手根据应用于模型的验证属性呈现,并且仅当您具有关联的 @Html.ValidationMessageFor()
并包含相关脚本文件时才有用(jquery、jquery.validate 和 jquery.validate。不显眼)。
如果您在模型中包含 [Required]
属性,您将获得服务器端和客户端验证 属性
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
并在视图中
@Html.TextBoxFor(m => m.AdvertModel.Title, new { @class = "form-control", placeholder = "Enter a descriptive title about the item for sale", title = "Enter a descriptive title" })
@Html.ValidationMessageFor(m => m.AdvertModel.Title)
旁注:您不需要 type="text"
(这是由助手添加的),我认为 id = "Enter a descriptive..."
是一个错字,它确实是 title = "Enter a descriptive..."
我的视图中有以下文本框
@Html.TextBoxFor(m => m.AdvertModel.Title, new
{
@class = "form-control",
type = "text",
placeholder = "Enter a descriptive title about the item for sale",
id = "Enter a descriptive title",
data_val_required = "The Title field is required.",
data_val = "true"
})
你可以看到我已经添加了 data_val_required 和 data_val 属性来完成它,呈现如下:
<input id="Enter a descriptive title" class="form-control" type="text" value="" placeholder="Enter a descriptive title about the item for sale" name="Title" data-val-required="The Title field is required." data-val-maxlength-max="100" data-val-maxlength="The field Title must be a string or array type with a maximum length of '100'." data-val="true">
当我 运行 应用程序和 this 并将其留空并单击提交时 ModelState.isValid 始终为真,当我期望它为假时,为什么它一直说它为真?
当您将表单提交给 POST 方法时,表单值包含 key/value 对,由每个控件的 name
属性和 value
属性组成。在您的情况下,它将是 AdvertModel.Title=The value entered in the textbox
。没有关于控件中其他属性的信息被发送到服务器。
data-*
属性由 html 助手根据应用于模型的验证属性呈现,并且仅当您具有关联的 @Html.ValidationMessageFor()
并包含相关脚本文件时才有用(jquery、jquery.validate 和 jquery.validate。不显眼)。
如果您在模型中包含 [Required]
属性,您将获得服务器端和客户端验证 属性
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
并在视图中
@Html.TextBoxFor(m => m.AdvertModel.Title, new { @class = "form-control", placeholder = "Enter a descriptive title about the item for sale", title = "Enter a descriptive title" })
@Html.ValidationMessageFor(m => m.AdvertModel.Title)
旁注:您不需要 type="text"
(这是由助手添加的),我认为 id = "Enter a descriptive..."
是一个错字,它确实是 title = "Enter a descriptive..."