如何将关系数据添加到ASP.net MVC数据实体创建视图?

时间:2021-01-07 04:13:01

Let's say I have a table of staff members, and a separate table of staff roles in a many-to-many relationship (ie, with a StaffMembersInRoles table in between). Using the ADO.net entity framework we have an object model like this: alt text http://martindoms.com/img/datamodel.png

假设我有一个工作人员表,以及一个多对多关系中的员工角色的单独表格(即,其间有一个StaffMembersInRoles表)。使用ADO.net实体框架,我们有一个这样的对象模型:alt text http://martindoms.com/img/datamodel.png

I have a StaffController controller with a create method and a Create view which is pretty much the standard automatically generated type. I want the create page to list the roles in the StaffRoles table with a checkbox next to each, and when the submit button is pressed these are added to the new StaffMember StaffRole IEnumable (and so the appropriate entries are made in the StaffMembersInRoles table). How would I go about this with a strongly-typed view?

我有一个带有create方法和Create视图的StaffController控制器,它几乎是标准的自动生成类型。我希望创建页面列出StaffRoles表中的角色,每个表旁边都有一个复选框,当按下提交按钮时,这些会被添加到新的StaffMember StaffRole IEnumable中(因此在StaffMembersInRoles表中输入相应的条目)。如何通过强类型视图来解决这个问题?

2 个解决方案

#1


Hi the Problem with this approach is you don't really have a strongly type entity passed to the View. In this problem you need the StaffMember information and a list of all StaffRole entities. PS: I really dun like the approach of casting the list in the view : StaffRole[])ViewData["AllRoles"]

嗨这个方法的问题是你没有真正传递给View的强类型实体。在此问题中,您需要StaffMember信息和所有StaffRole实体的列表。 PS:我真的很喜欢在视图中投射列表的方法:StaffRole [])ViewData [“AllRoles”]

Basicly i will prefer to work with DTO.

基本上我更愿意与DTO合作。

DTO:

public StaffMemberDto
{ 
    public int StaffMemberId { get; set; }
    public IList<StaffRoleDto> AllStaffRoles { get; set;}
    public IList<StaffRoleDto> MembersRolesAttached  { get; set;}
}

public StaffRoleDto
{
    public int RoleId {get; set;}
    public string RoleName { get; set; }
}

Controller:

return View(StaffMemberDto);

So in the view you get all roles strongly typed:

因此,在视图中,您将获得强类型的所有角色:

foreach (var role in ViewDate.Model.AllStaffRoles)
{
    ...
}

And in the post you can send the StaffMemberDto with the good RoleDto already assigned in the view or you can do the Request trick to get the id of the checkboxes ticked.

在帖子中,您可以使用已在视图中指定的良好RoleDto发送StaffMemberDto,或者您可以执行请求技巧以获取复选框的ID。

Well in a view like this i will probably use jquery to request an addRole each time someone tick the box to add a role. It will add some ajax to your form and you will not have some postback.

那么在这样的视图中,我可能会使用jquery在每次有人勾选框添加角色时请求addRole。它会为你的表单添加一些ajax,你不会有一些回发。

#2


This is how i'd do it:

我就是这样做的:

Firstly, you need an array of all possible roles. In the controller, i'd do something like this (i'm making some assumptions about your DAO):

首先,您需要一个包含所有可能角色的数组。在控制器中,我会做这样的事情(我对你的DAO做了一些假设):

ViewData["AllRoles"] = (StaffRole[])StaffRole.FindAll();

Then in your view, loop through the roles:

然后在您的视图中,循环遍历角色:

<% foreach (StaffRole Role in (StaffRole[])ViewData["AllRoles"]) { %>

 <p>
  <label>
   <%= Html.CheckBox("Role_"+Role.RoleId.ToString()) %>
   <%= Html.Encode(Role.RoleName) %>
  </label>
 </p>

<% } %>

Then in your POST controller, do something like this:

然后在POST控制器中,执行以下操作:

foreach (StaffRole Role in (StaffRole[])StaffRole.FindAll())
{
  if (Request.Params["Role_"+Role.RoleId.ToString()]=="true")
    MyStaff.Roles.Add(Role);
}

#1


Hi the Problem with this approach is you don't really have a strongly type entity passed to the View. In this problem you need the StaffMember information and a list of all StaffRole entities. PS: I really dun like the approach of casting the list in the view : StaffRole[])ViewData["AllRoles"]

嗨这个方法的问题是你没有真正传递给View的强类型实体。在此问题中,您需要StaffMember信息和所有StaffRole实体的列表。 PS:我真的很喜欢在视图中投射列表的方法:StaffRole [])ViewData [“AllRoles”]

Basicly i will prefer to work with DTO.

基本上我更愿意与DTO合作。

DTO:

public StaffMemberDto
{ 
    public int StaffMemberId { get; set; }
    public IList<StaffRoleDto> AllStaffRoles { get; set;}
    public IList<StaffRoleDto> MembersRolesAttached  { get; set;}
}

public StaffRoleDto
{
    public int RoleId {get; set;}
    public string RoleName { get; set; }
}

Controller:

return View(StaffMemberDto);

So in the view you get all roles strongly typed:

因此,在视图中,您将获得强类型的所有角色:

foreach (var role in ViewDate.Model.AllStaffRoles)
{
    ...
}

And in the post you can send the StaffMemberDto with the good RoleDto already assigned in the view or you can do the Request trick to get the id of the checkboxes ticked.

在帖子中,您可以使用已在视图中指定的良好RoleDto发送StaffMemberDto,或者您可以执行请求技巧以获取复选框的ID。

Well in a view like this i will probably use jquery to request an addRole each time someone tick the box to add a role. It will add some ajax to your form and you will not have some postback.

那么在这样的视图中,我可能会使用jquery在每次有人勾选框添加角色时请求addRole。它会为你的表单添加一些ajax,你不会有一些回发。

#2


This is how i'd do it:

我就是这样做的:

Firstly, you need an array of all possible roles. In the controller, i'd do something like this (i'm making some assumptions about your DAO):

首先,您需要一个包含所有可能角色的数组。在控制器中,我会做这样的事情(我对你的DAO做了一些假设):

ViewData["AllRoles"] = (StaffRole[])StaffRole.FindAll();

Then in your view, loop through the roles:

然后在您的视图中,循环遍历角色:

<% foreach (StaffRole Role in (StaffRole[])ViewData["AllRoles"]) { %>

 <p>
  <label>
   <%= Html.CheckBox("Role_"+Role.RoleId.ToString()) %>
   <%= Html.Encode(Role.RoleName) %>
  </label>
 </p>

<% } %>

Then in your POST controller, do something like this:

然后在POST控制器中,执行以下操作:

foreach (StaffRole Role in (StaffRole[])StaffRole.FindAll())
{
  if (Request.Params["Role_"+Role.RoleId.ToString()]=="true")
    MyStaff.Roles.Add(Role);
}