Asp.net mvc 设置ValidationMessageFor@Html.TextBox
我有以下代码:Asp.net mvc 设置ValidationMessageFor@Html.TextBox,asp.net-mvc,validation,razor,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 = "
@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())
}
使用(Html.BeginForm(“搜索”、“主页”))
{
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.AntiForgeryToken()
@TextBox(“Search_Data”,ViewBag.FilterValue作为字符串,new{@class=“Search box”,required=“required”})
@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);
}
[DbFunction(“ECdevEntities”,“Main_Search”)]
公共虚拟IQueryable主搜索(字符串关键字)
{
var keywordParameter=关键字!=null?
新的ObjectParameter(“关键字”,关键字):
新的ObjectParameter(“关键字”,typeof(字符串));
返回((IObjectContextAdapter)this.ObjectContext.CreateQuery(“[ECDeventies].[Main\u Search](@keyword)”,关键字参数);
}
这是我在模型中所做的
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>
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
名称
@Html.TextBoxFor(model=>model.ContactName,新的{@class=“textfield”})*
@Html.ValidationMessageFor(model=>model.ContactName)
主题
@Html.TextBoxFor(model=>model.Desc,新的{@class=“textfield”})*
@Html.ValidationMessageFor(model=>model.Desc)
你的电子邮件
@Html.TextBoxFor(model=>model.Email,新的{@class=“textfield”})*
@Html.ValidationMessageFor(model=>model.Email)
你的信息
@Html.TextAreaFor(model=>model.Message,新的{@class=“textarea”})
*
@Html.ValidationMessageFor(model=>model.Message)
}
希望它能在某种程度上帮助您……表单未提交,但我无法确定是否显示错误消息。??这会混淆您的表单是否提交?是的,如果有输入,它将提交,如果为空,它不会按预期提交,但我尝试在提交失败时显示一条错误消息。好的,我将给您一些示例代码,这些代码与您的示例不太相关,但会给您一个想法。我尝试过,但我的模型不是标准的。搜索_数据字段不是来自数据库,而是作为关键字值传递。请参见模型。您可以在模型中使用类似的内容,然后@if(ViewData.ModelState.IsValid){…}