0

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!

tereško
  • 58,060
  • 25
  • 98
  • 150
Tom
  • 1,051
  • 4
  • 21
  • 36
  • 1
    What exactly are you having an issue with? I can't tell what you understand, and don't understand about MVC so I'm uncertain how to help you. It would be useful to see what you've tried so far. – DMulligan Aug 11 '11 at 18:19
  • I've been using Create methods in my controllers which are pretty standard. I'm not sure how to only render the one control and save that data back into the model in the best way. I don't want to have to write an IF statement for every possible attribute in the controller to pass to the view. I.e. IF Task.Attribute = "Firstname" Then Do something to pass to the view I'd have to write a lot of code as there will be a lot of attributes. I'd normally set an attributes value by writing Person.Firstname = 'xx'. How can I relate the string Task.Attribute to the 'attribute' of the Person class? – Tom Aug 11 '11 at 20:57

1 Answers1

0

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.

DMulligan
  • 8,993
  • 6
  • 33
  • 34
  • It's not entirely an MVC issue as such but I'm using MVC and need to achieve the desired result. Thanks for the heads up on reflection, I'm reading into it now and I think I'll be able to achieve what I want! You've noted that there are some downsides with reflection; Is there perhaps a better way I could design my app to achieve the same result without reflection? Thanks for the advice! – Tom Aug 14 '11 at 16:54
  • @Tom Edited a suggestion into my answer. – DMulligan Aug 15 '11 at 04:32
  • Thanks for the extra suggestion. I'd actually been considering this approach and had already begun doing two sample implementations of each method. My hesitation / unknown with the PersonAttribute class is that I have an element of reporting which must be done out of the application. I can iterate through the attributes collection to build a report and columns but I imagine it will take a lot more effort than a normal dataset with defined columns etc. Thanks for the help so far, I'm grateful for the opportunity to bounce ideas off someone! – Tom Aug 15 '11 at 15:29