我无法找出问题所在。我尝试不允许将空值设置为字段,当用户尝试执行此操作时,它必须显示消息" *",并等待他设置值,但它不起作用并且null值成功发送到一个动作。 型号:
public class CompanyMainInfoModel
{
public int CompanyId { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "company_name", ResourceType = typeof(Localization))]
public string CompanyName { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "company_address", ResourceType = typeof(Localization))]
public string CompanyAddress { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "company_director", ResourceType = typeof(Localization))]
public string CompanyDirector { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "company_phone", ResourceType = typeof(Localization))]
public string CompanyTelephoneNumber { get; set; }
}
标记:
@model BTGHRM.Models.CompanyMainInfoModel
@{
Layout = "~/Views/Shared/_EmployeeMain.cshtml";
}
<head>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
</head>
<body>
<span class="content_h4">@Resources.Localization.company_info</span>
<br />
<br />
<div id="FormContainer">
@Html.Partial("Partial/_CompanyInfo", Model)
</div>
</body>
部分加价:
@model BTGHRM.Models.CompanyMainInfoModel
@using (Html.BeginForm("CompanyInfo", "Administration"))
{
<table>
<tr>
<td>@Html.LabelFor(m => m.CompanyName)</td>
<td>@Html.TextBoxFor(m => m.CompanyName)</td>
<td>@Html.ValidationMessageFor(m => m.CompanyName, "" , new { @class="text-danger"})</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.CompanyAddress)</td>
<td>@Html.TextBoxFor(m => m.CompanyAddress)</td>
<td>@Html.ValidationMessageFor(m => m.CompanyAddress, "", new { @class = "text-danger" })</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.CompanyDirector)</td>
<td>@Html.TextBoxFor(m => m.CompanyDirector)</td>
<td>@Html.ValidationMessageFor(m => m.CompanyDirector, "", new { @class = "text-danger" })</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.CompanyTelephoneNumber)</td>
<td>@Html.TextBoxFor(m => m.CompanyTelephoneNumber)</td>
<td>@Html.ValidationMessageFor(m => m.CompanyTelephoneNumber, "", new { @class = "text-danger" })</td>
</tr>
</table>
<input style="width:78px" type="submit" value=@Resources.Localization.save />
}
尽管[必需]它仍然允许我为任何字段设置空值。 我的代码出了什么问题?
答案 0 :(得分:2)
您需要添加&#34; jquery.validate.js&#34;和&#34; jquery.validate.unobtrusive.js&#34;在你的视图中。验证需要以上两个文件以及&#34; Jquery.js&#34;。希望这会有所帮助。
答案 1 :(得分:2)
您必须在Web配置中启用不显眼的脚本
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
然后你需要添加