【问题标题】:set ValidationMessageFor @Html.TextBox设置 ValidationMessageFor @Html.TextBox
【发布时间】:2014-12-02 19:40:26
【问题描述】:

我有以下代码:

@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);
}

【问题讨论】:

  • 表单未提交,但我无法弄清楚是否显示错误消息。??这让您的表单是否提交感到困惑?
  • 是的,如果有输入,它将提交,如果为空白,则不会按预期提交,但我正在尝试在提交失败时显示错误消息。
  • 好的,我会给你一些与你的例子不太相关但会给你一个想法的示例代码..

标签: asp.net-mvc validation razor


【解决方案1】:

这是我在我的模型中所做的

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>


         } 

希望对你有所帮助...

【讨论】:

  • 我试过了,但我的模型不是标准的。 Search_Data 字段不是来自数据库,而是作为关键字值传递。见模型。
  • 你可以在你的模型中使用类似的东西然后@if (ViewData.ModelState.IsValid) { ... }
猜你喜欢
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多