在模型绑定器启动之前(使用 DateTime),使用数据注释和 Linq-to-SQL 验证 MVC 2 表单

发布于 2024-08-22 04:57:56 字数 1221 浏览 7 评论 0原文

我正在使用带有数据注释的 linq to SQL 和 MVC2,并且在验证某些类型时遇到一些问题。

例如:

[DisplayName("Geplande sessies")]
[PositiefGeheelGetal(ErrorMessage = "Ongeldige ingave. Positief geheel getal verwacht")]
public string Proj_GeplandeSessies { get; set; }

这是一个整数,我正在验证从表单中获取正数。

public class PositiefGeheelGetalAttribute : RegularExpressionAttribute {
    public PositiefGeheelGetalAttribute() : base(@"\d{1,7}") { }
}

现在的问题是,当我在输入中写入文本时,我看不到此错误,但我从 modelbinder 收到错误消息,显示“值‘Tomorrow’对于 Geplande sessies 无效。”

控制器中的代码:

[HttpPost]
public ActionResult Create(Projecten p)
{
    if (ModelState.IsValid)
    {
        _db.Projectens.InsertOnSubmit(p);
        _db.SubmitChanges();

        return RedirectToAction("Index");
    }
    else
    {
        SelectList s = new SelectList(_db.Verbonds, "Verb_ID", "Verb_Naam");
        ViewData["Verbonden"] = s;
    }

    return View();
}

我想要的是能够在模型绑定器之前运行数据注释,但这听起来几乎不可能。我真正想要的是我自己写的错误消息显示在屏幕上。

我对 DateTime 有同样的问题,我希望用户以特定的形式“dd/MM/yyyy”编写,并且我有一个正则表达式。但同样,当数据注释完成其工作时,我得到的只是一个 DateTime 对象,而不是原始字符串。因此,如果输入不是日期,则正则表达式甚至不会运行,因为数据注释只是得到空值,因为模型绑定程序无法将其转换为日期时间。

有谁知道如何进行这项工作?

I'm using linq to SQL and MVC2 with data annotations and I'm having some problems on validation of some types.

For example:

[DisplayName("Geplande sessies")]
[PositiefGeheelGetal(ErrorMessage = "Ongeldige ingave. Positief geheel getal verwacht")]
public string Proj_GeplandeSessies { get; set; }

This is an integer, and I'm validating to get a positive number from the form.

public class PositiefGeheelGetalAttribute : RegularExpressionAttribute {
    public PositiefGeheelGetalAttribute() : base(@"\d{1,7}") { }
}

Now the problem is that when I write text in the input, I don't get to see THIS error, but I get the errormessage from the modelbinder saying "The value 'Tomorrow' is not valid for Geplande sessies."

The code in the controller:

[HttpPost]
public ActionResult Create(Projecten p)
{
    if (ModelState.IsValid)
    {
        _db.Projectens.InsertOnSubmit(p);
        _db.SubmitChanges();

        return RedirectToAction("Index");
    }
    else
    {
        SelectList s = new SelectList(_db.Verbonds, "Verb_ID", "Verb_Naam");
        ViewData["Verbonden"] = s;
    }

    return View();
}

What I want is being able to run the Data Annotations before the Model binder, but that sounds pretty much impossible. What I really want is that my self-written error messages show up on the screen.

I have the same problem with a DateTime, which i want the users to write in the specific form 'dd/MM/yyyy' and i have a regex for that. but again, by the time the data-annotations do their job, all i get is a DateTime Object, and not the original string. So if the input is not a date, the regex does not even run, cos the data annotations just get a null, cos the model binder couldn't make it to a DateTime.

Does anyone have an idea how to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2024-08-29 04:57:56

两个选项:

(1) 您可以创建一个 Projecten viewModel,其中所有字段都是字符串。这样,viewModel 将始终根据发布的数据创建,并且您的数据注释验证将始终被评估。显然,您可以使用 AutoMapper 将 viewModel 映射到正确类型的业务对象。

(2) 您可以对模型绑定器进行子类化。

Two options:

(1) You can make a Projecten viewModel where all fields are strings. This way the viewModel will always be created from the posted data and your dataannotations validation will always be evaluated. Obviously, you would then map the viewModel to your properly typed business objects maybe using AutoMapper.

(2) You can subclass the model binder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文