如何在asp.net MVC4中将集合从视图传递到控制器

时间:2017-08-20 11:53:21

标签: c# asp.net asp.net-mvc asp.net-mvc-4 entity-framework-6

我想将一系列技能ID从我的视图传递给控制器​​动作,我有一个技能的下拉列表:

         <select name="skills">
           <option value="0">Java</option>
           <option value="1">C++</option>
           <option value="2">Fortran</option>
           <option value="3">ASP</option>
         </select>

我希望用户可以从下拉列表中选择许多技能并将其值存储在集合中,即数组,然后将该数组发布到控制器中,如下所示[员工与技能有很多关系]:

 [HttpPost]
 public ActionResult AddEmp(Employee emp ,IEnumerable<Skills> skills )

 {

   DBCtx db=new DbCtx();
   db.employees.Add(emp);
   var emp_id=db.SaveChanges();

   var employee=db.employees.Find(emp_id);

   foreach(item in skills)
   {
      var skill = db.skills.Find(item);
      employee.skills.Add(skill);
   }
   db.SaveChanges();

   return View();
   } 

我如何能够实现这一点,提前感谢....

1 个答案:

答案 0 :(得分:0)

前端的选项很少。 Razor,Angular,Jquery ......为了简化以下示例中的内容,我使用了Razor视图。我不认为您需要将技能作为强类型对象传递,因为您只需要 Id 所选技能。同样在示例中,我将技能列表静态/硬编码到剃刀视图中,理想情况下它应该从后端绑定。

说让我们假设我们的员工视图模型如下:

 public class EmployeeViewModel
    {
        public EmployeeViewModel()
        {
            SelectedSkills=new List<int>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public List<int> SelectedSkills { get; set; }
    }


   public class Skills
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

然后我们的Controller(EmployeeController.cs)将是。(请在数据绑定到类之后忽略EF逻辑)

    public class EmployeeController : Controller
        {
            public ActionResult Index()
            {
                return View("Employee",new EmployeeViewModel());
            }
            [HttpPost]
            public ActionResult AddEmp(EmployeeViewModel employee)

            {

                var idOfEmployee=AddEmployee(employee);

                foreach (var item in employee.SelectedSkills)
                {
                    AddSkill(idOfEmployee,item);
                }


                return View("Employee");
            }
       private void AddSkill(int idOfEmployee, int skillId)
        {
            // your EF logic
        }

        private int AddEmployee(EmployeeViewModel emp)
        {
            // your EF logic, get your id of the inserted employee
            return 0;
        }
     }

然后我们的 Employee.cshtml 视图可能是

@using System.Web.UI.WebControls
@using WebApplication4.Controllers
@model  WebApplication4.Controllers.EmployeeViewModel
@{
    ViewBag.Title = "Employee";
}

<h2>Employee</h2>

@{var listItems = new List<Skills>
  {
      new Skills { Id = 0,Name="Java" },
      new Skills { Id = 1,Name="C++" },
      new Skills { Id = 2,Name="Fortran" }
  };
}
@using (Html.BeginForm("AddEmp", "Employee"))
{
    @Html.TextBoxFor(m => m.Name, new { autofocus = "New Employee" })
    <br/>
    @Html.ListBoxFor(m => m.SelectedSkills,
        new MultiSelectList(listItems, "Id", "Name",@Model.SelectedSkills)
        , new { Multiple = "multiple" })
    <input type="submit" value="Submit" class="submit"/>
}