【问题标题】:Comma delimited numeric value fails validation in asp.net MVC 4逗号分隔的数值在 asp.net MVC 4 中验证失败
【发布时间】:2014-04-16 02:40:07
【问题描述】:

我有这个模型类:

using System;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    public class PropertyModel
    {
        public int Id { get; set; }
        public String BuildingStyle { get; set; }
        public int BuiltYear { get; set; }
        [Range(1, 100000000, ErrorMessage = "Price must be between 1 and 100,000,000.")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0,0}")]
        [Display(Name = "Price")]
        public int Price { get; set; }
        public string AgentName { get; set; }
    }
}

还有这个控制器:

using System.Web.Mvc;
using MvcApplication1.Models;`

namespace MvcApplication1.Controllers
{
    public class PropertyController : Controller
    {
        public ActionResult Edit()
        {
            PropertyModel model = new PropertyModel
            {
                AgentName = "John Doe",
                BuildingStyle = "Colonial",
                BuiltYear = 1978,
                Price = 650000,
                Id = 1
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(PropertyModel model)
        {
            if (ModelState.IsValid)
            {
                //Save property info.              
            }

            return View(model);
        }         
    }
}

还有这个观点:

@model MvcApplication1.Models.PropertyModel
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (@Html.BeginForm())
{
    <text>Built Year: </text>@Html.TextBoxFor(m => m.BuiltYear)<br />
    <text>Building Style: </text>@Html.TextBoxFor(m => m.BuildingStyle)<br />
    <text>Agent Name: </text>@Html.TextBoxFor(m => m.AgentName)<br />
    <text>Price: </text>@Html.TextBoxFor(m => m.Price)<br />
    <input type="submit" value="Save" />
}

如果我输入的价格不带任何逗号,则 ModelState.IsValid 为真。但是,如果我以逗号分隔值的形式输入价格,则 ModelState.IsValid 为 false(请参见屏幕截图)。为了能够用逗号输入数值并通过模型验证,我需要做什么?我知道实现我自己的自定义模型绑定器是一个选项,但我想将其作为最后一个选项。谢谢你。请分享您的想法。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    这就是问题所在:

    public int Price { get; set; }
    

    原因是您的价格设置为int。如果您放置逗号,它会明确导致错误。如果你想要一个逗号,只需将你的 int 更改为 string 然后如果你要使用它进行计算,只需使用 split[','] 拆分逗号并使用 Convert.ToInt32() 将其转换为 int方法。

    【讨论】:

    • 谢谢,但是如果我将数据类型更改为字符串,我将无法为 int 使用一些开箱即用的注释,例如 [Range(1, 100000000)],对吗?
    【解决方案2】:

    您使用的是哪个版本的 MVC?

    当您在价格文本中显示逗号时,我建议您应该使用自定义 ModelBinder 来获取其值。

    【讨论】:

    • 这个问题的标题说它是MVC4。是的,我可以像我提到的那样做一个自定义 ModelBinder,但我想把它作为最后一个选项。
    • 所以我决定实现我自己的自定义模型绑定器。但我遇到了问题。我在这里有一个问题:stackoverflow.com/questions/23145780/…。你能看看吗?谢谢。
    【解决方案3】:

    您需要将 Price 的数据类型更改为字符串,以便它通过验证。但是,通过这样做,您将不得不进行一些额外的检查以查看传入的字符串是否确实是有效的 int。有点额外的工作,但还不错。

    【讨论】:

    • 谢谢。我有点不愿意将数据类型更改为字符串。它应该是数字数据类型。
    • 我明白了。但是,如果您真的想要逗号,则必须将其用作字符串并手动将其转换为 int 或使用自定义 ModelBinder。我不知道你还能怎么做。
    • 客户坚持要逗号功能,我没办法。我想知道我是否可以这样做:为输入定义公共字符串 PriceAsString,还定义公共 int Price,它在内部将 PriceAsString 转换为 int。因此,视图将具有 @Html.TextBoxFor(m=>m.PriceAsString)。但是,这可能也意味着我将无法使用 [Range(1, 100000000)] 开箱即用地注释 PriceAsString,并且必须进行一些自定义验证。
    • 是的,这将完成同样的事情。
    【解决方案4】:

    成功代码在这里 http://blog.rebuildall.net/2011/03/02/jQuery_validate_and_the_comma_decimal_separator

    in Model-> [Range(1, 100000000, ErrorMessage = "Price must be between 1 and 100,000,000.")]

    在视图中(javascript)->

     $.validator.methods.range = function (value, element, param) {
        var globalizedValue = value.replaceAll(",", "");
        return this.optional(element) || (globalizedValue >= param[0] && 
        globalizedValue <= param[1]);
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多