MVC Partial Model Updates

后端 未结 1 1514
旧时难觅i
旧时难觅i 2020-12-07 05:09

I often find myself in the situation where I only want to present and edit some fields from my model. Let\'s say I have a model that represts an address, perhaps I just want

1条回答
  •  借酒劲吻你
    2020-12-07 05:40

    Use the System.ComponentModel.DataAnnotations.MetadataType.

    Something like:

    public class BaseClassOfProperties
    {
       public string Name { get; set; }
    }
    
    public interface INameViewableProperties
    {
       [Display(name = "Your Name")]
       string Name { get; set; }
    }
    
    public interface INameHiddenProperties
    {
       //[scaffoldColumn(false)] this completely hides the fields
       [UIHint("Hidden")] // i think...
       string Name { get; set; }
    }
    
    [MetadataType(typeof(INameViewableProperties)]
    public class NameViewAbleProperties : BaseClassOfProperties
    {
    }
    
    [MetadataType(typeof(INameHiddenProperties)]
    public class NameHiddenProperties : BaseClassOfProperties
    {
    }
    

    0 讨论(0)
提交回复
热议问题