我有一个非常奇怪的问题,不引人注意的验证。
我有一个带有三个stirng属性的Required属性的模型。我在两个字段中使用TextBoxFor,在最后一个字段使用TextAreaFor。验证工作在TextArea上,但不在输入字段上。查看源代码,我可以看到验证添加的属性不在输入上,而是文本框中的ARE。此外,如果我在文本框中输入数据并提交,则发布和MVC模型绑定DOES正确地将ModelState.IsValid设置为false,ValidationSummary显示字段。
什么会导致验证无法在输入元素上起作用?
型号:
public class TestModel : IEmail {
public string BCCAddresses {
get;
set;
}
[Required]
public string BodyFormat {
get;
set;
}
public string CCAddresses {
get;
set;
}
[Required]
public string FromAddress {
get;
set;
}
[Required]
public string SubjectLineFormat {
get;
set;
}
}
查看:
@section head {
<script src="@Url.Content( "~/Scripts/jquery.1.7.1.min.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery.validate.min.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery.validate.unobtrusive.min.js" )" type="text/javascript"></script>
}
@{
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
@using ( Html.BeginForm( "Edit", "Email", FormMethod.Post ) ) {
<table>
<tr>
<td>From</td>
<td>@Html.TextBoxFor( x => x.FromAddress, new { style = "width: 500px;" } )</td>
</tr>
<tr>
<td>CC</td>
<td>@Html.TextBoxFor( x => x.CCAddresses, new { style = "width: 500px;" } )</td>
</tr>
<tr>
<td>BCC</td>
<td>@Html.TextBoxFor( x => x.BCCAddresses, new { style = "width: 500px;" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">Subject Line</td>
</tr>
<tr>
<td colspan="2">@Html.TextBoxFor( x => x.SubjectLineFormat, new { style = "width: 500px;" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">Body Form</td>
</tr>
<tr>
<td colspan="2">@Html.TextAreaFor( x => x.BodyFormat, new { style = "width: 500px; height: 200px" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">
<input id="ApplyChanges" type="submit" class="NavigationButton" value="Save Changes" />
<input id="CancelChanges" type="button" class="NavigationButton" value="Cancel Changes" />
</td>
</tr>
<tr>
<td colspan="2">
@Html.ValidationSummary()
</td>
</tr>
<tr><td colspan="2"> </td></tr>
</table>
}
浏览器来源:
<form action="/url" method="post">
<table>
<tbody><tr>
<td>From</td>
<td><input style="width: 500px;" id="FromAddress" name="FromAddress" value="" type="text"></td>
</tr>
<tr>
<td>CC</td>
<td><input style="width: 500px;" id="CCAddresses" name="CCAddresses" value="" type="text"></td>
</tr>
<tr>
<td>BCC</td>
<td><input style="width: 500px;" id="BCCAddresses" name="BCCAddresses" value="" type="text"></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">Subject Line</td>
</tr>
<tr>
<td colSpan="2"><input style="width: 500px;" id="SubjectLineFormat" name="SubjectLineFormat" value="" type="text"></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">Body Form</td>
</tr>
<tr>
<td colSpan="2"><textarea style="width: 500px; height: 200px;" id="BodyFormat" cols="20" rows="2" name="BodyFormat" data-val-required="The BodyFormat field is required." data-val="true"></textarea></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">
<input id="ApplyChanges" class="NavigationButton" disabled="disabled" value="Save Changes" type="submit">
<input id="CancelChanges" class="NavigationButton" value="Cancel Changes" type="button">
</td>
</tr>
<tr>
<td colSpan="2">
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display: none;"></li>
</ul></div>
</td>
</tr>
<tr><td colSpan="2"> </td></tr>
</tbody></table>
</form>
答案 0 :(得分:1)
我使用几乎相同的示例代码进行了快速测试,验证工作正常。我会开始剥离项目,直到找到与验证相冲突的内容。
这里的参考资料是我的测试结果:
控制器:
public ActionResult Index()
{
var model = new TestModel();
return View("Index", model);
}
我使用的测试模型没有继承自“IEmail”:
public class TestModel
{
public string BCCAddresses
{
get;
set;
}
[Required]
public string BodyFormat
{
get;
set;
}
public string CCAddresses
{
get;
set;
}
[Required]
public string FromAddress
{
get;
set;
}
[Required]
public string SubjectLineFormat
{
get;
set;
}
}
我的观点非常相似。请注意,我没有为脚本库实现一个部分,而是在w视图中直接引用它们。 EG:
<script src="@Url.Content("~/Scripts/jquery.1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
希望这有帮助。