设置ValidationMessageFor @ Html.TextBox

时间:2014-12-02 19:40:27

标签: asp.net-mvc validation razor

我有以下代码:

@using (Html.BeginForm("Search", "Home"))
{
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  @Html.AntiForgeryToken()
  @Html.TextBox("Search_Data", ViewBag.FilterValue as string, new { @class = "search-box", required = "required" })
  <input type="image" src="~/Images/Search.gif" alt="Find Everything Catholic" style="display:inline" />
  @Html.Raw(MyAsYouType.getHtml())
}

表单未提交但我无法确定是否显示错误消息。

我的模特:

[DbFunction("ECdevEntities", "Main_Search")]
public virtual IQueryable<Main_Search_Result> Main_Search(string keyword)
{
  var keywordParameter = keyword != null ?
    new ObjectParameter("keyword", keyword) :
    new ObjectParameter("keyword", typeof(string));
  return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<Main_Search_Result>("[ECdevEntities].[Main_Search](@keyword)", keywordParameter);
}

1 个答案:

答案 0 :(得分:0)

这是我在我的模型中完成的

public class ContactModel
    {
        [Required (ErrorMessage="Name is Required")]
        public string ContactName { get; set; }

        [Required(ErrorMessage = "Subject is Required")]
        public string Desc { get; set; }

        [Required(ErrorMessage = "Email Address is Required")]
        [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Email Address is not Valid")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required(ErrorMessage = "Message is Required")]
        public string Message { get; set; }

        public bool MessageSent { get; set; } 

        //public string PType { get; set; }
    }
}

这是我的表格..

@using (Html.BeginForm())
         {
            @Html.ValidationSummary(true) 
                <label for="contactname">Name</label>
                @Html.TextBoxFor(model => model.ContactName, new { @class = "textfield" })<span class="require"> *</span>
                @Html.ValidationMessageFor(model => model.ContactName) 
                  <label for="contactsubject">Subject</label> 
                @Html.TextBoxFor(model => model.Desc, new { @class = "textfield" })<span class="require"> *</span>
               @Html.ValidationMessageFor(model => model.Desc) 
                   <label for="contactemail">Your E-mail</label> 
                @Html.TextBoxFor(model => model.Email, new { @class = "textfield" })<span class="require"> *</span>
                @Html.ValidationMessageFor(model => model.Email) 

                  <label for="contactmessage">Your Message</label> 


              @Html.TextAreaFor(model => model.Message, new { @class = "textarea" })
                <span class="require"> *</span>
                  @Html.ValidationMessageFor(model => model.Message ) 
                    <a href="javascript:{}" onclick="document.getElementById('form0').submit(); return true;" class="button"> 
                        <span>Send Message</span></a>


         } 

希望它能在某种程度上帮助你......