none
MVC: how to add validation on Property as Entity in ViewModel? RRS feed

  • Question

  • I have NoteViewModel.cs that has following properties

    NoteViewModel.vb

    Public Class NoteViewModel
        Public Property noteList As List(Of FillPolicyNotesGridEntity)
        Public Property sortby As String
        Public Property note_id As Integer
        Public Property GetNote As GetNoteEntity
    End Class

    GetNoteEntity.vb

    ' <auto-generated>
    '     This code was generated from a template.
    '
    '     Manual changes to this file may cause unexpected behavior in your application.
    '     Manual changes to this file will be overwritten if the code is regenerated.
    ' </auto-generated>
    '------------------------------------------------------------------------------
    
    Imports System
    Imports System.Collections.Generic
    
    Partial Public Class GetNoteEntity
        Public Property note_id As Integer
        Public Property note_type_id As Integer
        Public Property note As String
        Public Property diary_date As Nullable(Of Date)
        Public Property diary_timekeeper_no As String
        Public Property current_note_count_per_policy As Nullable(Of Integer)
        Public Property DiaryFor As String
    
    End Class

    I have partial view in which I get all fields binded with GetNote property. Everything works well except for validations. I want to set Required field validation on GetNote.note_type_id and GetNote.noteproperties which is basically part of GetNoteEntity class, and this is auto-generated file. I went through other stackoverflow to find similar question, and found to use MetadataType, which didnt work.

    Can anyone please help here?


    Regards, Puneet Sharma Sr.Software Developer

    Saturday, March 26, 2016 1:58 PM

Answers

All replies

  • Hello,

    This question is more suited to the Entity Framework forum, would you like me to move this question to that forum?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 26, 2016 2:44 PM
  • Yes please. Thanks.

    Regards, Puneet Sharma Sr.Software Developer

    Saturday, March 26, 2016 6:00 PM
  • You shouldn't be doing anything with auto generated files.

    The ViewModel should be made up of custom objects that you made like a DTO. The DTO has the validation attributes for a property and the ViewModel make up of DTO(s) is sent into the View. The EF entities are mapped to DTO(s) and DTO(s) mapped back to EF entities for persistence with the DB using EF.

    You go any other way, and you are probably going to get burnt.

    Saturday, March 26, 2016 10:25 PM
  • Are you suggesting, that there is no other way around to add validation attributes to GetNote under GetNote property?

    Thanks


    Regards, Puneet Sharma Sr.Software Developer

    Sunday, March 27, 2016 7:58 AM
  • Are you suggesting, that there is no other way around to add validation attributes to GetNote under GetNote property?

    Thanks


    Regards, Puneet Sharma Sr.Software Developer

    You can do it. But if I were you, I would just leave the EF Entities at the DAL and not address EF directly in the MVC controller. But it's your show.

    Sunday, March 27, 2016 10:07 AM