【问题标题】:How to compare two values in MVC validation?如何比较 MVC 验证中的两个值?
【发布时间】:2015-09-07 06:55:32
【问题描述】:

我正在创建一个应用程序,其中包含年龄范围、体重范围、身高范围等。因此,在这些字段中,我为每个字段使用了两个文本框。例如,对于年龄范围,我将 Age From 和 Age To 这两个文本框用于其他属性,依此类推。但我正在尝试找到一个解决方案来比较 Age From 和 Age To 以检查 Age To 是否没有比 Age From 大,反之亦然。为了实现这一点,我从 NUGET 下载了 Foolproof 并这样做:

    [GreaterThan("WeightFrom",ErrorMessage="Please verify the Weight Range")]
    public string WeightTo { get; set; }

但是这个验证器检查该字段是否为必填字段。因为用户可能根本不填写此条件,但如果他们填写,那么我需要检查输入以确保 Weight To 大于 Weight From 值。所以,请告诉我如何实现这一点。有没有其他方法可以做到这一点?谢谢。

【问题讨论】:

  • 不清楚您的要求 - 您使用的属性可确保 WeightTo 大于 WeightFrom 的值,这似乎是您想要的,但将属性设置为 string 不会感觉 - 它们需要是数字类型,例如int
  • 将其作为字符串仍然在做同样的事情,但如果我将此重量范围留空,那么它仍然会使其填充为红色。但是这些范围字段的系统条件是可选的。
  • 然后你需要写你自己的验证属性(比如)[GreaterThanIf]
  • 感谢您的回复。实际上,我是 MVC 的新手,所以如果您能给我提供一个创建验证属性的示例,我将不胜感激。
  • this article 开始,这是创建验证属性的一个很好的指南,包括服务器端和客户端验证。如果您遇到问题,请发布您尝试过的代码,表明什么不起作用

标签: c# asp.net-mvc properties


【解决方案1】:

如果您使用“WeightTo”属性数据类型作为 int,那么您可以使用数据注释作为 [Range(0, 100)] 来调整权重范围。

【讨论】:

  • 没有范围很好,但问题是如果我没有在 Wight 范围文本框中输入任何值,那么它会要求我填写它。但要求并非如此。要求是,如果用户填写此 Weight From 和 Weight TO 文本框,那么我必须检查 Weight TO 是否大于 Weight From。
  • @barsan,您是否尝试过将数据类型设为可空; int??
  • 我刚才按照你的建议试过了,还是不行。
  • 您遇到了什么类型的错误?检查您的脚本文件夹是否有 jquery.validate.unobtrusive、jquery.validate、jquery.unobtrusive-ajax。您是否输入了正确的路径来引用脚本文件?因为这些是客户端验证。所以应该需要javascript。
【解决方案2】:

在您的 post 方法中,在那里进行检查并为 WeightTo 和 WeightFrom 使用可为空的整数。我在这里为你创建了一个 DotNetFiddle https://dotnetfiddle.net/ci0V2I

控制器

    [HttpGet]
    public ActionResult Index()
    {
        return View(new SampleModel());
    }


    [HttpPost]
    public ActionResult Index(SampleModel model)
    {       
        if(model.WeightFrom.HasValue && model.WeightTo.HasValue)
        {
            if(model.WeightFrom.Value < model.WeightTo.Value)
            {
                ModelState.AddModelError("", "Weight from most be smaller than Weight to. Please fix this error."); 
            }
        }

        if(!ModelState.IsValid)
        {
            return View(model); 
        }

        return View(new SampleModel());
    }

型号

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
    public class SampleModel
    {
        [Display(Name = "Weight Form")]
        public int? WeightFrom { get; set; }

        [Display(Name = "Weight To")]
        public int? WeightTo { get; set; }

        [Required]
        [Display(Name = "Name")]
        public string Name { get; set; }
    }
}

查看

@model HelloWorldMvcApp.SampleModel
@{
    Layout = null;
}


                @using (Html.BeginForm())
                {

                @Html.ValidationSummary()

                    <div class="form-group">
                        @Html.LabelFor(m => m.WeightFrom)
                        @Html.TextBoxFor(m => m.WeightFrom, new {@class="form-control"}) 
                        @Html.ValidationMessageFor(m => m.WeightFrom)
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(m => m.WeightTo)
                        @Html.TextBoxFor(m => m.WeightTo, new {@class="form-control"}) 
                        @Html.ValidationMessageFor(m => m.WeightTo)
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(m => m.Name)
                        @Html.TextBoxFor(m => m.Name, new {@class="form-control"}) 
                        @Html.ValidationMessageFor(m => m.Name)
                    </div>

                    <button type="submit" class="btn btn-success submit">Save</button>
                }

【讨论】:

    猜你喜欢
    • 2015-07-08
    • 2011-10-24
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多