Can Editor be told to ignore a field in a .NET model

Can Editor be told to ignore a field in a .NET model

RalphWardRalphWard Posts: 6Questions: 2Answers: 0
edited December 2016 in Free community support

I'm using Editor in a .NET MVC 5 Code first application. On of my Entity models sets up a foreign key relationship with

public class user {
       public int Id {get ; set; }
       public string first_name { get; set; }
        // Setting foreign key for code first model
        public virtual Site site { get; set; }
        public int site_Id { get; set; }
}

If I use my model for Editor it tries to select 'site' from the user table - can I add a DataAnnotation or similar to get Editor to ignore this field?

To work around this I've setup DTO's in a separate directory but they're almost identical to my Models as I can't change the naming convention at all.

And is there anyway to use a class in a JoinModel class without declaring it in there?

So instead of user / site example provided in the docco - do something like
public class JoinModel {
public user user { get; set; }
public site site { get; set; }
}

and have it pick up the values of the classes instead of trying to select 'user'

This question has an accepted answers - jump to answer

Answers

  • RalphWardRalphWard Posts: 6Questions: 2Answers: 0
    edited December 2016

    Update to this - I've edited the DataTables.dll Editor and Attribute class to do what I need. To Attribute I've added this:

        /// <summary>
        /// Direct Editor to ignore this field in the model
        /// </summary>
        [AttributeUsage(AttributeTargets.Property)]
        public class EditorIgnoreField : Attribute
        {
            /// <summary>
            /// Ignore Field in Editor
            /// </summary>
            [DefaultValue(false)]
            public bool Ignore { get; set; }
    
            /// <summary>
            /// Constructor for a field attribute defining an error message
            /// </summary>
            public EditorIgnoreField(bool _ignore)
            {
                Ignore = _ignore;
            }
        }
    
        /// <summary>
        /// Direct Editor to ignore this field in the model
        /// </summary>
        [AttributeUsage(AttributeTargets.Property)]
        public class EditorModelFromClass : Attribute
        {
            /// <summary>
            /// Create Model from subclass
            /// </summary>
            [DefaultValue(false)]
            public bool Model { get; set; }
    
            /// <summary>
            /// Constructor for a field attribute defining an error message
            /// </summary>
            public EditorModelFromClass(bool _model)
            {
                Model = _model;
            }
        }
    
    

    And I've added the following to Editor._FieldFromModel

                foreach (var pi in model.GetProperties())
                {
                    if (pi.IsDefined(typeof(EditorIgnoreField)))
                        if (pi.GetCustomAttribute<EditorIgnoreField>().Ignore)
                            continue;
    
                    if (pi.IsDefined(typeof(EditorModelFromClass)))
                        if (pi.GetCustomAttribute<EditorModelFromClass>().Model)
                        {
                            _FieldFromModel(pi.PropertyType, parent + pi.Name + ".");
                            continue;
                        }
    

    Now my DTO for a Join table is declared like this:

        public class JoinModelDTO
        {
            [EditorModelFromClass(true)]
            public user user { get; set; }
    
            [EditorModelFromClass(true)]
            public site site { get; set; }
        }
    

    and in My Models I can add

      [EditorIgnoreField(true)]
    

    above the virtual class names to ignore those

    Hopefully these changes haven't fundamentally broken something else...

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Thanks for posting this. Currently no, there is no option to have Editor ignore a parameter in a model. I like your solution though and will consider that for a future release.

    I don't believe what you have done above would break anything.

    Regards,
    Allan

  • RalphWardRalphWard Posts: 6Questions: 2Answers: 0

    One last change, I had to add a similar thing in the MJoin where it builds the fields from the model

This discussion has been closed.