MVC验证13-2个属性至少输入一项_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > MVC验证13-2个属性至少输入一项

MVC验证13-2个属性至少输入一项

 2014/5/5 19:36:29  Darren Ji  博客园  我要评论(0)
  • 摘要:有时候,我们希望2个属性中,至少有一个是必填,比如:usingCar.Test.Portal.Extension; namespaceCar.Test.Portal.Models{publicclassPerson{publicintId{get;set;}publicstringTelephone{get;set;}publicstringAddress{get;set;}}}.csharpcode,.csharpcodepre{font-size:small;color
  • 标签:MVC

有时候,我们希望2个属性中,至少有一个是必填,比如:

class="alt">using Car.Test.Portal.Extension;
 
namespace Car.Test.Portal.Models
{
    public class Person
    {
        public int Id { get; set; }
        public string Telephone { get; set; }
        public string Address { get; set; }
    }
}

如果让Telephone和Address属性中,有一个是必填项,需要自定义特性。

 

□ 自定义AtLeastOneAttribute特性,派生于ValidationAttribute类,并实现IClientValidatable接口

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
 
namespace Car.Test.Portal.Extension
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class AtLeastOneAttribute : ValidationAttribute, IClientValidatable
    {
        public string PrimaryProperty { get; private set; }
        public string SecondaryProperty { get; private set; }
 
        public AtLeastOneAttribute(string primaryProperty, string secondProperty)
        {
            PrimaryProperty = primaryProperty;
            SecondaryProperty = secondProperty;
        }
 
        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentCulture, this.ErrorMessageString, PrimaryProperty, SecondaryProperty);
        }
 
        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value);
            object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value);
            if (primaryValue != null || secondaryValue != null)
            {
                return true;
            }
            else
            {
                return false;
            }
            //return (primaryValue != null || secondaryValue != null);
 
            //稍稍改动以下还可以比较2个属性是否相等
            //if (!primaryValue.Equals(secondaryValue))
            //    return true;
            //else
            //    return false;
        }
 
        public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            string errorMessage = FormatErrorMessage("");
            ModelClientValidationRule rule = new ModelClientValidationRule()
            {
                ValidationType = "atleastone",
                ErrorMessage = errorMessage
            };
            rule.ValidationParameters.Add("primary", this.PrimaryProperty);
            rule.ValidationParameters.Add("secondary", this.SecondaryProperty);
            yield return rule;
        }
    }
}
 

 

□ 把自定义特性AtLeastOneAttribute打到类上

using Car.Test.Portal.Extension;
 
namespace Car.Test.Portal.Models
{
    [AtLeastOne("Telephone", "Address",ErrorMessage = "Telephone和Address至少填一项~~")]
    public class Person
    {
        public int Id { get; set; }
        public string Telephone { get; set; }
        public string Address { get; set; }
    }
}
 

 

□ PersonController

    public class PersonController : Controller
    {
        public ActionResult Index()
        {
            return View(new Person());
        }
 
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Person person)
        {
            if (ModelState.IsValid)
            {
                //TODO:
                return Content("ok");
            }
            else
            {
                return View(person);
            }
        }
    }

 

□ Person/Index.cshtml

需要把自定义的特性注册到jquery相关类库中。

@model Car.Test.Portal.Models.Person
 
<body>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/jquery-migrate-1.2.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    @*<script src="~/Scripts/atleastone.js"></script>*@
    <style type="text/css">
        .validation-summary-errors {
            color: red;
        }
    </style>
    <script type="text/javascript">
        jQuery.validator.addMethod("atleastone", function (value, element, params) {
            var primaryProperty = params.primary;
            var secondaryProperty = params.secondary;
 
            if (primaryProperty.length > 0 || secondaryProperty.length > 0) {
                return true;
            } else {
                return false;
            }
        });
 
        jQuery.validator.unobtrusive.adapters.add("atleastone", ["primary", "secondary"], function (options) {
            options.rules["atleastone"] = {
                primary: options.params.primary,
                secondary: options.params.secondary
            };
            options.messages["atleastone"] = options.message;
        });
        
    </script>
    
    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
       
    
        <fieldset>
            <legend>Person</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Telephone)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Telephone)
                @Html.ValidationMessageFor(model => model.Telephone)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Address)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
 
</body>
</html>
 

 

□ 效果

至少填一项

上一篇: .Net中把图片等文件放入DLL中,并在程序中引用 下一篇: 没有下一篇了!
发表评论
用户名: 匿名