帮助创建视图和更新实体模型

发布于 2024-10-21 10:22:38 字数 361 浏览 1 评论 0原文

编辑:这个问题很模糊,可能对任何人都没用。我正在将互联网资金奖励给“帮助”我的下面的先生。

抱歉,标题有点模糊。我对 ASP MVC 和 ASP 还很陌生。 EF。这是我的问题。我有一个类似这样的数据库。

Employee
 - ID
 - Name
 - Address

EmployeeJob
 - EmployeeID
 - JobID
 - StartDate

JobTypes
  - ID
  - JobName

我想要一个创建表单,该表单将显示所有员工字段以及供用户选择的工作类型列表。然后我将结果发送回控制器并更新数据库。我已经开始研究自定义视图模型,但仍然不确定如何将其组合在一起并使其发挥作用。谢谢,

EDIT: This question is vague and will likely be of little use to anyone. I am awarding internet monies to the gentleman below whom "helped" me.

Sorry that the title is a little vague. I am still new to asp mvc & EF. Here is my issue. I have a DB somewhat like this.

Employee
 - ID
 - Name
 - Address

EmployeeJob
 - EmployeeID
 - JobID
 - StartDate

JobTypes
  - ID
  - JobName

I want a create form that will show all the Employee fields as well as a list of JobTypes for the users to be able to select. Then I will post the results back to the controller and update the DB. I have started looking into custom viewmodels, but am still unsure of exactly how to put that together and get it to work. Thanks,

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

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

发布评论

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

评论(1

月亮邮递员 2024-10-28 10:22:38

创建一个特定的视图模型,反映视图显示界面所需的数据。在本例中,员工信息加上当前关系所代表的职位集合。添加显示作业菜单所需的信息——在本例中,我将使用 SelectListItems 的枚举(值 = ID,文本 = JobName)。

public class EmployeeViewModel
{
    public Employee Employee { get; set; }
    public IEnumerable<string> CurrentJobs { get; set; }
    public int JobType { get; set; } // placeholder for post back value from menu
    public IEnumerable<SelectListItem> JobTypes { get; set; }
}

根据您想要执行的操作,您可能需要不同的帖子模型。

public class EmployeeJobAddition
{
    public Employee Employee { get; set; }
    public int JobType { get; set; }
}

或者(这可能需要在视图模型中使用不同的 JobTypes 集合)

public class EmployeeJobChange
{
    public Employee Employee { get; set; }
    public IEnumerable<int> CurrentJobTypes { get; set; }  // returns values to keep
    public IEnumerable<int> JobTypes { get; set; } // new jobs to add
}

使用视图模型提供的数据,使用输入名称设置表单以反映回发的模型。

Create a specific view model that reflects the data needed by the view to display the interface. In this case, the employee information plus the collection of jobs represented by the current relations. Add to that the information required to show the jobs menu -- in this case I'd use an enumeration of SelectListItems (Value = ID, Text = JobName).

public class EmployeeViewModel
{
    public Employee Employee { get; set; }
    public IEnumerable<string> CurrentJobs { get; set; }
    public int JobType { get; set; } // placeholder for post back value from menu
    public IEnumerable<SelectListItem> JobTypes { get; set; }
}

Depending on what you are trying to do you might want a different model for the post.

public class EmployeeJobAddition
{
    public Employee Employee { get; set; }
    public int JobType { get; set; }
}

or (this might argue for a different collection for JobTypes in the view model)

public class EmployeeJobChange
{
    public Employee Employee { get; set; }
    public IEnumerable<int> CurrentJobTypes { get; set; }  // returns values to keep
    public IEnumerable<int> JobTypes { get; set; } // new jobs to add
}

Set up your form with input names to reflect the model being posted back, using the data supplied by the view model.

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