.NET MVC 3如何循环模型状态错误并强制ModelState.Valid或禁用特定错误?

时间:2012-05-02 12:55:28

标签: c# asp.net-mvc model-view-controller validation action-filter

我正在使用MVC 3 +不引人注意的验证。

对于某些领域,我也在使用远程验证;在远程验证中,我做了一些可以返回错误或只是警告的检查(我想利用ajax验证只是为了发出警告,而不仅仅是阻止错误)。 我通过描述文本中的“Info”前缀将验证错误与警告区分开来。

那么,是否存在循环所有验证错误的方法,仅显示警告并根据显示的文本设置错误?

我在考虑使用ActionFilterAttribute,或者在循环并检查所有验证错误后强制ModelState.Valid = true ......

以下是我的远程验证例程的摘录,其中包含WarningCheck属性:

   [WarningCheck]
   public JsonResult CheckMyField(string myfield) 
    {

        //....some check...if ok I do `return Json(true, JsonRequestBehavior.AllowGet);`
        //...if just a warning, I do the follow...

        string warn = String.Format(CultureInfo.InvariantCulture,
            "Info: some info....");
        ModelState.AddModelError(TicketHD, esiste);
        return Json(warn, JsonRequestBehavior.AllowGet);

    }

    [AttributeUsage(AttributeTargets.All)]
    public class WarningCheckAttribute : ActionFilterAttribute
    {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
       {  
             //.... here I'd like to cycle my warnings and if possible maintaining just the text display and set errors off...is it possible?
       }
     }

编辑(如何禁用特定警告的客户端验证)

关于服务器端的@CDSmith(@Alfalfastrange)建议,当每个验证错误中包含特定文本时,我也成功禁用了客户端验证。特别是,我的需求是禁用客户端和当错误包含“Info:”文本时,服务器端验证错误。 这是我用于客户端行为的代码:

       .... 
                    $("#ticketfrm").submit();
                    var isvalid = true;
                    var errmark = $("#ticketfrm .field-validation-error span"); 
                    $(errmark).each(function () {
                        var tst = $(this).text();
                        if (!(tst.indexOf("Info") != -1))
                            isvalid = false; //if the val error is not a warn, the it must be a real error!
                        });

                    if (isvalid) {
                        var form = $('#ticketfrm').get(0); //because I'm inside a jquery dialog
                         $.removeData(form, 'validator'); 
                        jQuery('#ticketfrm').unbind('submit').submit();
                    }

                    $("#ticketfrm").submit();   
                }
            .....

我希望这可以帮助很多人......我花了很多时间来完成这项工作!我不认为它必须是更优雅的解决方案,但它的工作原理! :) 在进行服务器端验证时,请阅读标记的解决方案。

如果您觉得这很有用,请标记。谢谢!

1 个答案:

答案 0 :(得分:6)

我不确定我是否理解这个问题以及你正在解释但我听到你说的是你需要一种通过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;
}

我希望能为您提供您所寻找的东西,它的工作方式我认为我理解您想要的。让我知道