简体   繁体   中英

mvc Controller Architecture and modelbinding

My User entity has numerous differing properties which define the User record. After the default scaffolded edit and create pages are created we are now trying to implement some regions to the pages so similar areas of the users profile can be edited and updated without posting back and refreshing the entire list of properties.

I was thinking of splitting the regions into separate partial views like below and then using @Ajax.BeginForm(

    public partial class UserContact : UserBase
    {
        [DataType(DataType.EmailAddress)]
        [StringLength(255)]        
        public string EmailAddress { get; set; }

        [DataType(DataType.PhoneNumber)]
        [StringLength(50)]
        public string PhoneHome { get; set; }
            ...
    }
    public partial class UserAddress : UserBase
    {
        [StringLength(60)]
        public string AddressLine1 { get; set; }

        [StringLength(60)]
        public string AddressLine2 { get; set; }
            ...
    }
    public partial class UserBase
    {
        [Key]
        [Required(ErrorMessage = "User is required")]
        public System.Guid UserId { get; set; }
    }

just spotted the binding keyword and i was wondering which methods people use. I would imagine its not very efficient over the wire and both in terms of the necessary validation to post back an entire Usermodel each time so do people split the main model into seperate models, or is it possible (or even adviseable) with the bind parameter to specify only a subset of the properties?

In my opinion it is indeed advisable to split the model into multiple sub models, however you also need to split your actions into sub actions. Each action will be 'bound' to that sub class and not the entire UserBase class.

If you use only one action, I don't think it is possible to [dynamically] specify what properties to bind and which not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM