2015-11-05 70 views
0

我的主页索引显示大量数据,并且还包含提交表单。这个表单很好用......除了......验证错误总是存在的。MVC重置模型状态,以便错误消息不显示

enter image description here

当用户成功提交表单,并传递ModelState.IsValid方法我重定向他们回到指数(入口点)操作方法。在这一点上,我想所有的验证不显示。他们刚刚提交了为什么此时显示错误?请参阅下面的代码(高亮部分显示了我目前试图删除验证。

enter image description here

当用户第一次进入屏幕没有验证消息(好),但第一个提交后,他们永远不会消失再次

我怎样才能得到验证,以阻止一次我重定向到指数()谢谢 编辑:?

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>

该表格总是显示验证错误。

@ataravati - 我重定向到索引,因为我想重新显示他们刚刚在相同的页面。表单将重置,数据将相同 - 这是想要的行为。表单实际上发送一封电子邮件,它不应该将它们重定向到一个不同的页面。

编辑2 - 我想,如果我删除我的自定义错误消息,表单按预期工作。现在弄清楚为什么/如何避开它。

+0

显示你的代码,而不是它的一个形象。重定向到Index()时,不会显示验证消息。您的方法没有模型作为参数,所以没有添加到“ModelState”中。如果你看到这些错误是因为你返回视图,而不是重定向(你永远不会输入'if(ModelState.IsValid)'块) –

+0

为什么当ModelState有效时重定向到Index? – ataravati

+0

事实上,即使我第一次停止应用并打开主页,它似乎总是显示验证错误。我会更新一些代码。 – BrianLegg

回答

1

指定使用ValidationMessageFor()的第二个参数显示的错误。相反,使用

@Html.ValidationMessageFor(m => m.Name, null, new { @class = "text-danger" }) 

和组使用数据标注错误消息文本上你的属性

[Required(ErrorMessage = "A name is required.")] 
public string Name { get; set; } 
+0

工作很好,谢谢! – BrianLegg