MODEL
public class SearchTerm
{
[Required(ErrorMessage="please enter")]
public string SearchTrm { get; set; }
}
查看
@using (@Html.BeginForm())
{
@Html.ValidationSummary();
@Html.AntiForgeryToken();
....
@Html.TextBoxFor(m=>m.SearchTrm)</span>
<input type="submit" value="Search"/>
@Html.ValidationMessageFor(m=>m.SearchTrm)
@using (Html.BeginForm("Search","Home"))
{
@Html.DropDownList("SelectedFieldId", new SelectList(Model.Fields, "FieldID", "NiceName", Model.SelectedFieldId));
}
}
控制器
[HttpPost]
public ActionResult Search(SearchTerm Model)
{
// some code here....
}
当我点击一个空的搜索时,我希望验证消息发生,但页面正在回发,我正在NullReferenceException
答案 0 :(得分:0)
提及脚本名称@section scripts {...}并检查jqueryval是否有2个文件--~ / scripts / jquery.validate.min.js“,”〜/ scripts / jquery.validate.unobtrusive.min。 JS “” 〜/脚本