Dynamic UI for MVC3
I am new in MVC(3). I am trying to look for an idea on how I can render UIs with dynamic controls and is it possible to have it bind with a model/class that is dynamic as well?
Example: UI has First Name, Addres开发者_Go百科s and State (Code only) field from the Member table. Model/Class contains First Name, Address and State field.
User wants to view State Name but comes from a different table (States table) so I need to add State Name (where State Code=State Code in the Member table) to the UI as well as the model/class.
Additional fields will be based on the tables who has foreign key relationships with the main table.
Any insight will be highly appreciated.
Thanks.
As long as your model contains that field then it will be bound. I'm not 100% following how you mean dynamic though. Think of it this way - the model binder will take posted fields from your form and match their names to a property in the model.
If a property doesn't exist in the model, it won't be found and hence not bound. You 'could' write a method that parses additional fields from your FormsCollection (ie Request.Form["parameter name"]) and puts it into some dynamic collection in your model, however this could get very messy.
Generally with MVC you want to just add your new field to a ViewModel which is essentially a model that you are making specific to that view. You can use a tool like AutoMapper http://automapper.codeplex.com/ to map (copy) property values between your ViewModel and your real model.
精彩评论