我正在尝试解决以下问题。我有一个存储数据的基类,比如说一个人。
创建新人员时,只会通过表单填写其中一些详细信息,即名字、姓氏。当该人被保存到数据库时,它也会被分配一个 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
我还有一个名为 Task 的类,它将控制一个人的其余属性的输入。
任务被分配给用户并要求他们完成 Person 类的单个属性。
Class Task
Public Property ID As Integer
Public Property ParentPerson As Person
Public Property AssignedUser As User
Public Property Attribute As String
我想知道如何才能最好地实现打开任务、加载属性文本框然后将其保存回数据库的能力?
提前致谢!
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!
发布评论
评论(1)
听起来您的问题与 MVC 无关,听起来您正在寻找一种方法来获取给定匹配字符串名称的属性名称。
我相信还有其他方法可以做到这一点,但我知道的方法就是反思。警告:我一直被告知反射是缓慢的、危险的,除非必要否则不应该使用。
也许你的数据模型会像这样更好
然后你可以通过
id == Task.Attribute
上的 where 查询来获取你想要的 PersonAttribute参见 http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ 或在编辑器上进行搜索模板来了解如何将复杂列表绑定到 mvc 中的控制器。
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.
Perhaps your data model would be better off like this
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.