1

我在 asp.net MVC-3 中开发我的项目。

在这些模块中,我创建了一个用于创建团队的视图。其中有一个项目名称下拉列表。和一个列表框(ListBox-1),用于从下拉列表中显示所选项目的成员。并且还有另一个列表框(ListBox-2)用于传输/选择其中的选定或所有记录(成员姓名)。这是使用javascript完美完成的。

我必须将所有数据保存在该表格的数据库中。因为我必须保存显示在数据库中的第二个列表框(ListBox-2)中的团队成员列表。这些我的问题是如何获取该列表框(ListBox-2)的数据并将其保存在数据库字段中?

下面是我的创建团队视图... 在此处输入图像描述

在这种形式中,第一个下拉列表显示项目名称并且下拉列表下方有两个列表框,用于显示项目的成员名称

列表框的查看代码如下..

Dropdownlist for Project
  <%: Html.DropDownListFor(model => model.availableproject, new SelectList(ViewBag.Projects as System.Collections.IEnumerable, "project_id", "project_name"),"Select Project", new { id = "ddlCars" })   %> 


code of listbox.
 <div class="ddlModels" style="float:left; ">
                              <%--  <%: Html.ListBox("projectmembers_id") %>  --%>
                                 <%: Html.ListBoxFor(Models => Models.availableuser, new SelectList(Enumerable.Empty<SelectListItem>(), "user_id", "user_login_name"),
                             new { id = "ddlModels2" }) %>


                            </div>

                            <div style="display:inline; float:left; margin-left:5px; margin-right:5px; margin-top:10px;">
                                <input type="button" name="add" id="alladd"  onclick="CopyListsall();" value=">>" style="width:27px; height:15px; font-size:8px; color:#56a1d6;"/><br />
                                <input type="button" name="add" id="add" onclick="CopyLists();"value=">" style="width:27px; height:15px; font-size:8px; color:#56a1d6;"/><br /><br />
                                <input type="button" name="remove" id="allremove"  onclick="RemoveListsall();" value="<<" style="width:27px; height:15px; font-size:8px; color:#56a1d6;" /><br />                
                                <input type="button" name="remove" id="remove"  onclick="RemoveLists();"  value="<" style="width:27px; height:15px; font-size:8px; color:#56a1d6;" />
                            </div>

                            <div class="ddlModels" style="float:left;">
                             <%: Html.ListBoxFor(Models => Models.availableuser, new SelectList(Enumerable.Empty<SelectListItem>(), "user_id", "user_login_name"),
                             new { id = "ddlModels1" }) %>
                             <%-- <%: Html.ListBox("projectmembers_id") %>--%>
                            </div>

模型代码(.cs 文件)

namespace ProjectManagementSystem.Models
{
    public class TeamCreate
    {

        [Required(ErrorMessage = "Empty Not Allow")]
        public int team_id { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        [DisplayName("Team Name")]
        public string team_name { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int project_id { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public string project_name { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int projectmembers_id { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int projectmemberuser_id { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int user_id { get; set; }


        public int SerialNumber { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        [DisplayName("Team Leader")]
        public string user_real_name { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int technology_skill_id { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public string technology_skill_name { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public string team_description { get; set; }

        [Required(ErrorMessage = "Empty Not Allow")]
        public int leader_id { get; set; }


        public bool t1 { get; set; }

        public List<user_master> availableuser;
        public List<project_master> availableproject;
        public List<team_master> availableteam;
        public List<team_to_user> availableteamuser;

        public List<technology_skill_master> availabletechnology;

        public List<int> SelectSkill { get; set; }
        public List<int> lbtechnologyskillname2 { get; set; }

        public List<int> ddlModels { get; set; }
        public List<int> ddlModels1 { get; set; }

    }
}

第二个.cs文件来获取数据..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectManagementSystem.Models
{
    public class TeamCreateFetch
    {
        private int counter = 1;


        //ProjectManagementSystemEntities3 pb = new ProjectManagementSystemEntities3();
        public static IList<TeamCreate> all()
        {     

            IList<TeamCreate> result =
                (IList<TeamCreate>)HttpContext.Current.Session["team1"];

            if (result == null)
            {
                ProjectManagementSystemEntities3 pb = new ProjectManagementSystemEntities3();

                HttpContext.Current.Session["team1"] = result =

                    ( from l in pb.team_master
                     join u in pb.user_master
                     on l.user_id equals u.user_id
                     select new TeamCreate
                     {
                         //SerialNumber= counter++,
                         team_id= l.team_id,
                         user_id = u.user_id,
                         team_name= l.team_name,
                         user_real_name=u.user_real_name,


            }

            return result;
        }
    }
}

我的数据库如下...

表名 - team_master

team_id
team_name
user_id(FK with user_id of "user_master")
team_description

表 2 名称-team_to_user

user_id(PK)
user_real_name

表 3 名称-team_to_user

team_id (FK with team id of "team_master")
user_id (FK with user_id of "user_master")

现在我必须将第二个 ListBox 的值保存在 user_id 字段的表“team_to _user”中。

那么如何在 MVC-3 中做到这一点?

提前致谢

4

1 回答 1

0

我建议取消双列表框并将其实现为单个 MultiSelectListBox。

该模型

public class MyClass {
    [Required]
    [Display(Name = "Items Selected (Use ctl + click to select multiple)")]
    public int[] SelectedItems { get; set; }
    public IEnumerable<ItemDTO> Items { get; set; }
}

public class ItemDTO { 
    public int ID { get; set; }
    public string Name { get; set; }
}

风景

@Html.LabelFor(model => model.SelectedItems):                                                     
@Html.ListBoxFor(model => model.SelectedItems, new MultiSelectList(Model.Items, "ID", "Name", Model.SelectedItems))
@Html.ValidationMessageFor(model => model.SelectedItems)

在您的控制器中,只需遍历 SelectedItems 数组并保存每个

于 2012-04-09T11:31:00.487 回答