我的主頁索引顯示大量數據,並且還包含提交表單。這個表單很好用......除了......驗證錯誤總是存在的。MVC重置模型狀態,以便錯誤消息不顯示
當用戶成功提交表單,並傳遞ModelState.IsValid方法我重定向他們回到指數(入口點)操作方法。在這一點上,我想所有的驗證不顯示。他們剛剛提交了爲什麼此時顯示錯誤?請參閱下面的代碼(高亮部分顯示了我目前試圖刪除驗證。
當用戶第一次進入屏幕沒有驗證消息(好),但第一個提交後,他們永遠不會消失再次
我怎樣才能得到驗證,以阻止一次我重定向到指數()謝謝 編輯:?
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 - 我想,如果我刪除我的自定義錯誤消息,表單按預期工作。現在弄清楚爲什麼/如何避開它。
顯示你的代碼,而不是它的一個形象。重定向到Index()時,不會顯示驗證消息。您的方法沒有模型作爲參數,所以沒有添加到「ModelState」中。如果你看到這些錯誤是因爲你返回視圖,而不是重定向(你永遠不會輸入'if(ModelState.IsValid)'塊) –
爲什麼當ModelState有效時重定向到Index? – ataravati
事實上,即使我第一次停止應用並打開主頁,它似乎總是顯示驗證錯誤。我會更新一些代碼。 – BrianLegg