I'm trying to solve the following problem. I have a base class which stores data, lets say for a Person. When a new Person is created, only some of those details will be filled in via a form i.e. Firstname, Lastname. When the person is saved to the DB, it also gets assigned an ID.
Class Person
Public Property ID As Integer
Public Property Firstname As String
Public Property Lastname As String
Public Property Age As Integer
Public Property Location As String
Public Property Gender As String
I also have a class called Task which will control the input of the remaining attributes of a person. A task is assigned to a User and requires them to complete a single attribute of the Person class.
Class Task
Public Property ID As Integer
Public Property ParentPerson As Person
Public Property AssignedUser As User
Public Property Attribute As String
I'm wondering how I can best achieve the ability to open a Task, load the textbox for the Attribute then save this back into the db?
Thanks in advance!
It doesn't sound like your issue is with MVC, it sounds like you're looking for a way to get a property name given a matching string name.
I believe there are other ways to do this, but the one I know is reflection. Warning: I've always been told reflection is slow, dangerous, and shouldn't be used unless necessary.
Type t = typeof(Person);
FieldInfo f = t.getField(Task.Attribute);
string oldValue = (string) f.GetValue(person); //gets old string given Person person
f.SetValue(person, "xx"); // sets value of person.Take.Attribute to "xx"
Perhaps your data model would be better off like this
Class Person
Public Property ID As Integer
Public Property Attributes As List<PersonAttribute>
Class PersonAttribute
Public Property Id as String //FirstName, LastName, etc
Public Property Value as String
Public Property DisplayName as String //if you need some label for html pages
Then you could just fetch the PersonAttribute you want with a where query on id == Task.Attribute
See http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ or do a search on editor templates to see how you can bind a complex list to a controller in mvc.