4

我正在使用 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) 建议,当每个验证错误中包含特定文本时,我也成功禁用了客户端验证。特别是,我需要在错误包含“信息:”文本时禁用客户端和服务器端验证错误。这是我用于客户端行为的代码:

       .... 
                    $("#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();   
                }
            .....

我希望这可以帮助很多人......我工作了很多小时才能让它工作!我不认为它一定是更优雅的解决方案,但它可以工作!:) 对于服务器端验证,请阅读标记的解决方案。

如果你觉得这个有用,请标记它。谢谢!

4

1 回答 1

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;
}

我希望这能给你你想要的东西,它的工作方式我认为我理解你想要的。让我知道

于 2012-05-13T12:58:25.840 回答