ASP.NET MVC - 动态类属性

发布于 2024-11-29 06:39:56 字数 686 浏览 1 评论 0 原文

我正在尝试解决以下问题。我有一个存储数据的基类,比如说一个人。 创建新人员时,只会通过表单填写其中一些详细信息,即名字、姓氏。当该人被保存到数据库时,它也会被分配一个 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-06 06:39:57

听起来您的问题与 MVC 无关,听起来您正在寻找一种方法来获取给定匹配字符串名称的属性名称。

我相信还有其他方法可以做到这一点,但我知道的方法就是反思。警告:我一直被告知反射是缓慢的、危险的,除非必要否则不应该使用。

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"

也许你的数据模型会像这样更好

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

然后你可以通过 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文