首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置ValidationMessageFor @Html.TextBox

设置ValidationMessageFor @Html.TextBox
EN

Stack Overflow用户
提问于 2014-12-03 03:40:27
回答 1查看 1.6K关注 0票数 0

我有以下代码:

代码语言:javascript
复制
@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())
}

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

我的模型:

代码语言:javascript
复制
[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);
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-03 04:16:39

这是我在我的模型中做的

代码语言:javascript
复制
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; }
    }
}

这是我的表格..

代码语言:javascript
复制
@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>


         } 

希望它能在一定程度上帮助你……

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27257573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档