MVC3 动态 DataAnnotation 属性 StringLength

发布于 2024-12-03 15:40:27 字数 813 浏览 2 评论 0原文

更新#3:整个问题

我有一个HB类:

    public class HB
{
    public int Id { get; set; }

    [StringLength(3000)]
    public string Text { get; set; }
    public Title Title { get; set; }

}

和标题:

    public class Title
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int MaxChar { get; set; }   
}

在你写HB(这是一篇文章)之前,你必须选择你的标题,所以你的HB的StringLength。文本可以确定。这意味着,这篇文章只能有一定数量的字符,具体取决于作者的“标题”。示例:Title1 只能写入 1000 个字符的“HB”,Title2 可以写入 3000 个字符的“HB”。所以。这意味着 StringLength 必须来自 Title.MaxChar。最聪明的方法是什么?

Title 实体是将存储在数据库中的前缀数据。

说得非常清楚,我想要实现的目标是: [StringLength(Title.MaxChar)]

我已经在 Webforms 中为这种机制完成了一百万次结构/设计,我的大脑就是无法适应 mvc,所以一些帮助将不胜感激。代码将更加感激。

UPDATE #3: Entire question

I have a class HB:

    public class HB
{
    public int Id { get; set; }

    [StringLength(3000)]
    public string Text { get; set; }
    public Title Title { get; set; }

}

And Title:

    public class Title
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int MaxChar { get; set; }   
}

Before you can write a HB (which is kind of an article), you have to choose your title, so your StringLength for HB.Text can be determined. Meaning, this article can only have a certain amount of chars, deppending on what 'Title' the writer has. Example: Title1 can only write a 'HB' with 1000 chars, and Title2 can write a 'HB' with 3000 chars. So. Thats means the the StringLength has to come from Title.MaxChar. Whats the smartest way to do that?

The Title entity is prefixed data that will be stored in the db.

To be crystal clear, what I want to achieve is something in the line with: [StringLength(Title.MaxChar)]

Ive done structure/design for this mechanism in Webforms a million times, my brain just cant addapt to mvc, so some help would be appreciated. Code would be even more appreciated.

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

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

发布评论

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

评论(1

一身骄傲 2024-12-10 15:40:27

很确定这是不可能的。在我看来,这是试图将业务逻辑强制纳入属于控制器的模型中。

在这种情况下,我将在 Text 属性 [StringLength(3000)] 上创建该属性。在控制器中,在验证期间,我会写一些类似的内容:

public ActionResult (HB model)
{
    if (model.Text.Length > model.Title.MaxChar){
        ModelState.AddModelError("Text", string.Format("Text for this Title cannot exceed {0} characters.", model.Title.MaxChar));
    }

    if (ModelState.IsValid) 
    {
        //do stuff
        return RedirectToAction("Index"); //or something
    } 
    else 
    {
        return View(model);
    }
}

我相信这将完成您想要做的事情。现在,对于 Title 对象,我会在您的模型中将其展平一点:

public class HB
{
    #region Base Properties

    public int Id { get; set; }

    [StringLength(3000)]
    public string Text { get; set; }

    #endregion

    #region Title Properties

    public int TitleId { get; set; }
    public string TitleName { get; set; }
    public int TitleMaxChar { get; set; }

    #endregion
}

这是假设您需要在视图中显示该信息。如果您只需要引用它来进行业务逻辑验证,只需拥有 TitleId 属性,并在需要时使用它在控制器中实例化 Title 对象。如果这些属性不可编辑,请不要忘记为它们创建隐藏输入!

Pretty sure that is not possible as written. This strikes me as trying to force business logic into the model that belongs in the controller.

In this situation, I would make the attribute on the Text property [StringLength(3000)]. In the controller, during validation, I would write something along these lines:

public ActionResult (HB model)
{
    if (model.Text.Length > model.Title.MaxChar){
        ModelState.AddModelError("Text", string.Format("Text for this Title cannot exceed {0} characters.", model.Title.MaxChar));
    }

    if (ModelState.IsValid) 
    {
        //do stuff
        return RedirectToAction("Index"); //or something
    } 
    else 
    {
        return View(model);
    }
}

I believe this will accomplish what you are trying to do. Now, for the Title object, I'd flatten that out a bit in your model:

public class HB
{
    #region Base Properties

    public int Id { get; set; }

    [StringLength(3000)]
    public string Text { get; set; }

    #endregion

    #region Title Properties

    public int TitleId { get; set; }
    public string TitleName { get; set; }
    public int TitleMaxChar { get; set; }

    #endregion
}

This is assuming you need to display that information in your view. If you just need to reference it for your business logic validation, just have the TitleId property and use that to instantiate the Title object in your controller when you need it. Don't forget to make hidden inputs for each of these properties if they are not editable!

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