mvc4中的验证没有发生

时间:2016-04-16 08:55:41

标签: validation asp.net-mvc-4

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

1 个答案:

答案 0 :(得分:0)

提及脚本名称@section scripts {...}并检查jqueryval是否有2个文件--~ / scripts / jquery.validate.min.js“,”〜/ scripts / jquery.validate.unobtrusive.min。 JS “” 〜/脚本