我的主页索引显示了大量数据,还包括提交表单。表单效果很好......除了......验证错误始终存在。
当用户成功提交表单并传递ModelState.IsValid方法时,我将它们重定向回Index(入口点)操作方法。此时我希望不显示所有验证。他们刚刚提交了为什么显示错误?请参阅下面的代码(突出显示的部分显示了我当前尝试删除验证。
当用户第一次进入屏幕时,没有验证消息(好),但在第一次提交后,它们永远不会再消失。
一旦重定向到Index(),如何才能停止验证?谢谢。 编辑:
public ActionResult Index()
{
var model = new PublicationModel(PublicationRepository.Instance.GetAllPublications().OrderBy(p => p.pname).ToList());
return View(model);
}
[HttpPost]
public ActionResult Index(PublicationSubmitItem model)
{
if (ModelState.IsValid)
{
//Send an email
return RedirectToAction("Index");
}
var parentModel = new PublicationModel(PublicationRepository.Instance.GetAllPublications().OrderBy(p => p.pname).ToList());
parentModel.SubmitItem = model;
return View(parentModel);
}
<section id="pubform">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<fieldset>
<legend></legend>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@*@Html.HiddenFor(model => Model.Id)*@
<div class="form-group">
@Html.LabelFor(model => model.SelectedPublications, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.SelectedPublications, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.SelectedPublications, "You must pick at least 1 publication.", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control medium-control" } })
@Html.ValidationMessageFor(model => model.Name, "A name is required.", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control medium-control" } })
@Html.ValidationMessageFor(model => model.Email, "An email address is required.", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RoomNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RoomNumber, new { htmlAttributes = new { @class = "form-control short-control" } })
@Html.ValidationMessageFor(model => model.RoomNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LocationId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.LocationId, new SelectList(Model.Locations, "Id", "Name"))
@Html.ValidationMessageFor(model => model.LocationId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Extension, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Extension, new { htmlAttributes = new { @class = "form-control short-control" } })
@Html.ValidationMessageFor(model => model.Extension, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-9 col-md-10">
<input type="submit" value="Send" class="btn btn-default" style="background-color: #DAEAF5;" />
<input type="button" value="Clear" class="btn btn-default" style="background-color: #DAEAF5;" id="clearButton" />
</div>
</div>
</fieldset>
</div>
}
</section>
&#13;
表单始终显示验证错误。
@ataravati - 我重定向到索引,因为我想重新显示他们刚才所在的页面。表单将重置,数据将相同 - 这是想要的行为。该表单实际上发送了一封电子邮件,它不应该将它们重定向到其他页面。
EDIT2 - 我发现如果删除自定义错误消息,表单将按预期工作。现在弄清楚为什么/如何解决它。
答案 0 :(得分:1)
使用ValidationMessageFor()
的第二个参数指定要显示的错误。相反,使用
@Html.ValidationMessageFor(m => m.Name, null, new { @class = "text-danger" })
并使用属性上的数据注释设置错误消息文本
[Required(ErrorMessage = "A name is required.")]
public string Name { get; set; }