我不确定我是否像您解释的那样理解这个问题,但我听到您说的是您需要一种循环遍历 ModelState 错误然后维护错误的文本值但不将它们显示为错误的方法?你是这么说的吗?
对于初学者来说,ModelState 只不过是一个 DictionaryList,您可以毫无问题地遍历它。
像这样的东西可以做到这一点:
public ActionResult SomeAction(SomeModel model) {
if(ModelState.IsValid) {
// do cool stuff with model data
}
var errorMessages = GetModelStateErrors(ModelState);
foreach (var errorMessage in errors) {
// do whatever you want with the error message string here
}
}
ModelError 包含一个 ErrorMessage 属性和 Exception 属性
internal static List<string> GetModelStateErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
var errors = new List<ModelError>();
errors = modelStateDictionary.SelectMany(item => item.Value.Errors).ToList();
}
不确定这是否有帮助,但如果它指向正确的方向,那么很酷:-)
更新
好的,这就是我想出的,它适用于我的测试应用程序。
首先让我列出我拥有的东西,以便您可以复制和复制
这是我的模型
public class EmployeeViewModel {
public int ID { get; set; }
[Display(Name = "First Name")]
[Required(ErrorMessage = "Error")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Error")]
public string LastName { get; set; }
[Display(Name = "Username")]
public string Username { get; set; }
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
}
这是一个使用此模型的简单视图
@model TestApp.Models.EmployeeViewModel
<h2>Test</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Username)
@Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是我使用的控制器和操作
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TestApp.Models;
namespace TestApp.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return RedirectToAction("Test");
}
public ActionResult Test() {
var model = new EmployeeViewModel();
return View(model);
}
[HttpPost]
public ActionResult Test(EmployeeViewModel model) {
// Force an error on this property - THIS should be the only real error that gets returned back to the view
ModelState.AddModelError("", "Error on First Name");
if(model.EmailAddress == null) // Add an INFO message
ModelState.AddModelError("", "Email Address Info");
if (model.Username == null) // Add another INFO message
ModelState.AddModelError("", "Username Info");
// Get the Real error off the ModelState
var errors = GetRealErrors(ModelState);
// clear out anything that the ModelState currently has in it's Errors collection
foreach (var modelValue in ModelState.Values) {
modelValue.Errors.Clear();
}
// Add the real errors back on to the ModelState
foreach (var realError in errors) {
ModelState.AddModelError("", realError.ErrorMessage);
}
return View(model);
}
private IEnumerable<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
var errorMessages = new List<ModelError>() ;
foreach (var keyValuePair in modelStateDictionary) {
if (keyValuePair.Value.Errors.Count > 0) {
foreach (var error in keyValuePair.Value.Errors) {
if (!error.ErrorMessage.Contains("Info")) {
errorMessages.Add(error);
}
}
}
}
return errorMessages;
}
}
}
如果您愿意,可以将 GetRealErrors 编写为 LINQ:
private IEnumerable<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
var errorMessages = new List<ModelError>() ;
foreach (var keyValuePair in modelStateDictionary.Where(keyValuePair => keyValuePair.Value.Errors.Count > 0)) {
errorMessages.AddRange(keyValuePair.Value.Errors.Where(error => !error.ErrorMessage.Contains("Info")));
}
return errorMessages;
}
我希望这能给你你想要的东西,它的工作方式我认为我理解你想要的。让我知道