[英]How to pass collection from view to controller in asp.net MVC4
I want to pass a collection of skills id from my view to controller action, i have a dropdownlist of SKILLS : 我想将一组技能ID从我的视图传递到控制器操作,我有一个SKILLS的下拉列表:
<select name="skills">
<option value="0">Java</option>
<option value="1">C++</option>
<option value="2">Fortran</option>
<option value="3">ASP</option>
</select>
i want that user can select many skills from dropdown and store their value in a collection ,ie an array, and then post that array to action in controller as follow [employee and skills have a manytomany relationship]: 我希望用户可以从下拉列表中选择许多技能,并将其值存储在一个集合中,即数组,然后按如下方式将该数组发布到控制器中以进行操作[员工和技能有很多关系]:
[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();
}
How can i achieve this ,thanks in advance .... 我要如何实现这一点,在此先感谢...。
You have quite few options on front end . 您在前端的选择很少。 Razor, Angular, Jquery... To simplfy things in following example i have used Razor view.
Razor,Angular,Jquery ...为了简化下面的示例,我使用了Razor视图。 I dont think you need to pass Skills as a strongly type object as you only need Id of selected Skills .
我认为您不需要通过技能作为强类型对象,因为您只需要选定技能的 ID 。 Also in the example i have the skills list static / hard coded into razor view, ideally it should be bound from backend.
同样在示例中,我将技能列表静态/硬编码到剃刀视图中,理想情况下,它应从后端绑定。
Saying that lets assume our Employee View Model as it follows 假设我们遵循以下员工视角模型
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; }
}
Then our Controller (EmployeeController.cs) would be .(please ignore the EF logic after data is bound to class) 然后我们的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;
}
}
Then our Employee.cshtml view could be 然后我们的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"/>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.