我試圖一起使用ModelState.AddErrrorModel
和Html.ValidationMessage("Account")
以顯示錯誤消息。儘管向ModelState添加了消息,但Html.ValidationMessage仍爲空
但是,即使我可以調試模型錯誤正在我的POST上添加,我不能在視圖上再次加載視圖時看到它。從形式,在那裏我可以看到ModelState.AddModelError
正在呼籲POST
代碼運行(ex.Message確實具有價值和):
try {
// code that fails in this case
}
catch (Exception ex)
{
logger.Error(ex);
ModelState.AddModelError("Register",ex.Message);
}
return RedirectToAction("Account", accountViewModel);
我的看法是這樣的:
<h3>Get your free account now</h3>
@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "form" }))
{
// trying to test different methods to get my errors... NOTHING works :)
@Html.ValidationSummary()
@Html.ValidationMessage("Register")
@Html.BusinessErrorMessageBox()
<div class="form-group">
<label>Email address</label>
@Html.TextBoxFor(m => m.RegisterViewModel.Email, new { @class = "form-control", placeholder = "Email", @type = "email" })
@Html.ValidationMessageFor(m => m.RegisterViewModel.Email)
</div>
<div class="form-group">
<label>Password</label>
@Html.PasswordFor(m => m.RegisterViewModel.Password, new { @class = "form-control", placeholder = "Password" })
@Html.ValidationMessageFor(m => m.RegisterViewModel.Password)
</div>
<div class="form-group">
<label>Your country (the country where you live or where your organization is registered in)</label>
@Html.DropDownListFor(model => model.RegisterViewModel.SelectedCountry, Model.RegisterViewModel.Countries, new { @class = "form-control" })
</div>
<input type="submit" class="btn btn-primary btn-lg" value="Get your free account now" />
}
出於實際的原因,我的觀點被稱爲帳戶,而接受POST的方法被稱爲「註冊」。
但無論我做什麼,在ValidationMessage
或ValidationSummary
中都沒有顯示錯誤。
任何想法我做錯了什麼?
編輯:
FYI:有在視圖兩種形式,如果可能有一個influrence。
編輯2:
我可以看到單擊該轉到我的服務器的按鈕時,下面的HTML被輸出。價值ex.Message
,顯示無處:
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div><span class="field-validation-valid" data-valmsg-for="Business" data-valmsg-replace="true"></span><span class="field-validation-valid" data-valmsg-for="Register" data-valmsg-replace="true"></span>
你試過'ModelState.AddModelError(string.Empty,ex.Message);'用'@ Html.ValidationSummary(true)' –
剛剛試過。不幸的是,這並沒有幫助。僅供參考 - 頁面上也有兩種形式 –