MVC 中强类型视图的自定义条件验证

发布于 2024-12-09 19:30:19 字数 876 浏览 0 评论 0原文

我有一个 Person 模型和一个学生模型。学生模型有 2 个 PersonID 的 FK;一份供学生使用,另一份供家长使用。

我的观点如下所示:

  @Html.EditorFor(m => m.student.Person.FirstName)
  @Html.EditorFor(m => m.student.Person.DOB)

  @Html.EditorFor(m => m.student.Father.FirstName)

模型如下所示:

  public partial class Person
  {
    public int PersonID { get; set; }

    [Required]
    [PlaceHolder("First Name")]
    public string FirstName { get; set; }

    [PlaceHolder("Birth Date")]
    public Nullable<System.DateTime> DOB { get; set; }
  }


 public partial class Student
 {
    public int Student_PersonID { get; set; }
    public int Parent_PersonID { get; set; }
 }

我希望 DOB 成为学生的必填字段,但不是家长的必填字段。如果我将 [Required] 属性添加到 DOB 元素,那么两者都需要它。有没有办法在视图上设置需要字段?或者模型中有没有办法或使用验证属性来做到这一点?

仅供参考...我正在使用 EF 数据库第一种方法,

谢谢

I have a Person model and a student model. The student model has 2 FKs of PersonIDs; one for student and the other for parent.

My view looks like this:

  @Html.EditorFor(m => m.student.Person.FirstName)
  @Html.EditorFor(m => m.student.Person.DOB)

  @Html.EditorFor(m => m.student.Father.FirstName)

The models would look like this:

  public partial class Person
  {
    public int PersonID { get; set; }

    [Required]
    [PlaceHolder("First Name")]
    public string FirstName { get; set; }

    [PlaceHolder("Birth Date")]
    public Nullable<System.DateTime> DOB { get; set; }
  }


 public partial class Student
 {
    public int Student_PersonID { get; set; }
    public int Parent_PersonID { get; set; }
 }

I want the DOB to be required field for the student but not for the parent. If I add [Required] attribute to the DOB element, then it requires it for both. Is there a way I can set a require a field on the view? or is there a way in the model or using validation attribute to do this?

fyi... i am using EF database first approach

thanks

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

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

发布评论

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

评论(2

山色无中 2024-12-16 19:30:19

我建议让视图模型与视图中显示的字段相匹配。如果稍后要从视图中删除某个字段,那么它也将从域模型中删除。

在这种情况下,如果您的视图要显示以下字段:

  • StudentFirstName
  • StudentDOB
  • ParentFirstName
  • ParentDOB

那么我建议使用以下视图:

public class PersonViewModel
{
   public int StudentPersonID { get; set; }

   [Required]
   public string StudentFirstName { get; set; }

   [Required]
   public DateTime StudentDOB { get; set; }

   public int ParentPersonID { get; set; }

   [Required]
   public string ParentFirstName { get; set; }

   public DateTime ParentDOB { get; set; }
}

或者如果您有 2 个单独的视图显示:

  • StudentFirstName
  • StudentDOB

并显示:

  • ParentFirstName
  • ParentDOB

那么我建议使用 2单独的视图模型:

public class StudentViewModel
{
   public int StudentPersonID { get; set; }

   [Required]
   public string StudentFirstName { get; set; }

   [Required]
   public DateTime StudentDOB { get; set; }
}

public class ParentViewModel
{
   public int ParentPersonID { get; set; }

   [Required]
   public string ParentFirstName { get; set; }

   public DateTime ParentDOB { get; set; }
}

以这种方式使用视图模型将允许您将 [Required] 数据注释用于需要它们的字段,而不是尝试创建解决方法。请注意,不要将视图模型与域模型混淆,因此需要将这些数据映射到域模型。

希望这有帮助。

如果您的应用程序是一个简单的应用程序,您可能不需要创建单独的业务逻辑层,并且大多数书籍仅提供具有简单模型的 MVC,这可能没问题。但是,如果您四处搜索,您会发现其他示例,开发人员建议将视图模型与业务模型分开,例如 这个

我还建议阅读Wrox Professional Enterprise .Net 2009 其中第 7 章和; 8 给出了业务层的很好的例子,并讨论了事务脚本模式、活动记录模式和域模型模式。

I would suggest having the view model match the fields that are displayed in the view. If later a field is to be removed from the view, then it will also be removed from the domain model.

In this case, if your view is to display the following fields:

  • StudentFirstName
  • StudentDOB
  • ParentFirstName
  • ParentDOB

Then I would suggest having the following view:

public class PersonViewModel
{
   public int StudentPersonID { get; set; }

   [Required]
   public string StudentFirstName { get; set; }

   [Required]
   public DateTime StudentDOB { get; set; }

   public int ParentPersonID { get; set; }

   [Required]
   public string ParentFirstName { get; set; }

   public DateTime ParentDOB { get; set; }
}

Or if instead you have 2 seperate views displaying:

  • StudentFirstName
  • StudentDOB

AND displaying:

  • ParentFirstName
  • ParentDOB

Then I would suggest having 2 seperate view models:

public class StudentViewModel
{
   public int StudentPersonID { get; set; }

   [Required]
   public string StudentFirstName { get; set; }

   [Required]
   public DateTime StudentDOB { get; set; }
}

public class ParentViewModel
{
   public int ParentPersonID { get; set; }

   [Required]
   public string ParentFirstName { get; set; }

   public DateTime ParentDOB { get; set; }
}

Using the view models in this way will allow you to use the [Required] data annotations for the fields that require them rather than trying to create a workaround. Note that the view models are not to be confused with the domain models and therefore this data would then need to be mapped to the domain model.

Hope this helps.

If your application is a simple application you may not need to create a seperate business logic layer and most books only present MVC with simple models which may be fine. However, if you search around you will find other examples where developers recommend having a view model seperate from a business model such as this

I would also recommend reading Wrox Professional Enterprise .Net 2009 where chapters 7 & 8 give great examples of the business layer with discussions of the Transaction Script pattern, Active Record pattern and Domain Model pattern.

韵柒 2024-12-16 19:30:19

一种方法是创建一个继承自 Person 的 PersonRequired 类。将元数据类添加到 PersonRequired,以便您拥有 PersonRequiredMetaData,并且在该特定情况下继承的 DOB 字段是必需的。您需要手动复制 Person 和 PersonRequired 类之间的值或使用 AutoMapper。我希望有比这更好的答案!

另一种选择是使用 FluentValidation ,它可以让您与模型分开进行验证(不使用数据注释)。我想知道是否有人使用数据注释来满足数据库需求,并使用流畅的验证来满足编程需求。

One way is to make a PersonRequired class that inherits from Person. Add a metadata class to PersonRequired so you have PersonRequiredMetaData and in that specific that the inherited DOB field is required. You would need to manually copy the values between the Person and PersonRequired classes or use AutoMapper. I hope there is a better answer than this!

Another option is to use FluentValidation that would let you do the validation separate from the model (doesn't use data annotations). I wonder if some people are using data annotations for database requirements and fluent validation for programmatic requirements.

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