ASP .NET MVC 5 新创建值的复选框循环
ASP .NET MVC 5 Checkbox loop for new created values
我有三个实体 classes:
namespace App.Data.Models
{
public class Employee
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Project
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class EmployeeProject
{
[Key]
public int Id { get; set; }
[ForeignKey]
public int EmployeeId { get; set; }
[ForeignKey]
public int ProjectId { get; set; }
public virtual Employee Employee{ get; set; }
public virtual Project Project { get; set; }
}
}
和一个视图模型class
namespace App.Data.Models
{
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public List<SelectListItem> Projects { get; set; }
}
}
现在我可以添加新员工和新项目。我正在使用 localDB 和 Entity Framework。这是我的代码 https://github.com/IzaGorska/Project
的 link
我想要做的是在 EmployeeCreate 视图中为每个项目添加复选框(需要记住我没有任何硬编码项目 - 创建新项目将意味着额外的复选框)。
同样在 EmployeeDetails 视图中,我只想显示选中的项目。我该怎么做?
我有员工数据库和项目数据库。项目数据库:
namespace App.Data.Services
{
public interface IProjectData
{
IEnumerable<Project> GetAll();
Project Get(int id);
void Add(Project project);
void Update(Project project);
}
public class InMemoryProjectData : IProjectData
{
List<Project> projects;
public void Add(Project project)
{
projects.Add(project);
project.Id = projects.Max(e => e.Id) + 1;
}
public void Update(Project project)
{
var exist = Get(project.Id);
if (exist != null)
{
exist.Name = project.Name;
exist.Description = project.Description;
}
}
public Project Get(int id)
{
return projects.FirstOrDefault(e => e.Id == id);
}
public IEnumerable<Project> GetAll()
{
return projects.OrderBy(e => e.Name);
}
}
public class SQLProjectData : IProjectData
{
private readonly AppDbContext db;
public SQLProjectData(AppDbContext db)
{
this.db = db;
}
public void Add(Project project)
{
db.Projects.Add(project);
db.SaveChanges();
}
public Project Get(int id)
{
return db.Projects.FirstOrDefault(p => p.Id == id);
}
public IEnumerable<Project> GetAll()
{
return from p in db.Projects
orderby p.Name
select p;
}
public void Update(Project project)
{
var entry = db.Entry(project);
entry.State = EntityState.Modified;
db.SaveChanges();
}
}
}
控制器:
public partial class EmployeeController : Controller
{
private readonly AppDbContext _db;
public EmployeeController(AppDbContext db)
{
_db = db;
}
private List<SelectListItem> GetProjectListItems()
{
return _db.Projects.Select(p=>
new SelectListItem {
Text = p.Name,
Value = p.Id.ToString()
}).ToList();
}
public IActionResult CreateEmployee()
{
var employeeViewModel=new EmployeeViewModel
{
Employee=new Employee(),
Projects=GetProjectListItems()
}
return View(employeeViewModel);
}
}
在视图中,您可以使用此代码创建复选框列表:
@using App.Data.Models
@model EmployeeViewModel
.... @Model.Employee input controls
<ul>
@for (var i = 0; i < Model.Projects.Count; i++)
{
<li>
<input type="checkbox" asp-for="@Model.Projects[i].Selected" />
<label asp-for= "@Model.Projects[i].Selected"> @Model.Projects[i].Name</label>
<input type="hidden" asp-for="@Model.Projects[i].Value" />
<input type="hidden" asp-for="@Model.Project[i].Text" />
</li>
}
</ul>
我有三个实体 classes:
namespace App.Data.Models
{
public class Employee
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Project
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class EmployeeProject
{
[Key]
public int Id { get; set; }
[ForeignKey]
public int EmployeeId { get; set; }
[ForeignKey]
public int ProjectId { get; set; }
public virtual Employee Employee{ get; set; }
public virtual Project Project { get; set; }
}
}
和一个视图模型class
namespace App.Data.Models
{
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public List<SelectListItem> Projects { get; set; }
}
}
现在我可以添加新员工和新项目。我正在使用 localDB 和 Entity Framework。这是我的代码 https://github.com/IzaGorska/Project
的 link我想要做的是在 EmployeeCreate 视图中为每个项目添加复选框(需要记住我没有任何硬编码项目 - 创建新项目将意味着额外的复选框)。 同样在 EmployeeDetails 视图中,我只想显示选中的项目。我该怎么做?
我有员工数据库和项目数据库。项目数据库:
namespace App.Data.Services
{
public interface IProjectData
{
IEnumerable<Project> GetAll();
Project Get(int id);
void Add(Project project);
void Update(Project project);
}
public class InMemoryProjectData : IProjectData
{
List<Project> projects;
public void Add(Project project)
{
projects.Add(project);
project.Id = projects.Max(e => e.Id) + 1;
}
public void Update(Project project)
{
var exist = Get(project.Id);
if (exist != null)
{
exist.Name = project.Name;
exist.Description = project.Description;
}
}
public Project Get(int id)
{
return projects.FirstOrDefault(e => e.Id == id);
}
public IEnumerable<Project> GetAll()
{
return projects.OrderBy(e => e.Name);
}
}
public class SQLProjectData : IProjectData
{
private readonly AppDbContext db;
public SQLProjectData(AppDbContext db)
{
this.db = db;
}
public void Add(Project project)
{
db.Projects.Add(project);
db.SaveChanges();
}
public Project Get(int id)
{
return db.Projects.FirstOrDefault(p => p.Id == id);
}
public IEnumerable<Project> GetAll()
{
return from p in db.Projects
orderby p.Name
select p;
}
public void Update(Project project)
{
var entry = db.Entry(project);
entry.State = EntityState.Modified;
db.SaveChanges();
}
}
}
控制器:
public partial class EmployeeController : Controller
{
private readonly AppDbContext _db;
public EmployeeController(AppDbContext db)
{
_db = db;
}
private List<SelectListItem> GetProjectListItems()
{
return _db.Projects.Select(p=>
new SelectListItem {
Text = p.Name,
Value = p.Id.ToString()
}).ToList();
}
public IActionResult CreateEmployee()
{
var employeeViewModel=new EmployeeViewModel
{
Employee=new Employee(),
Projects=GetProjectListItems()
}
return View(employeeViewModel);
}
}
在视图中,您可以使用此代码创建复选框列表:
@using App.Data.Models
@model EmployeeViewModel
.... @Model.Employee input controls
<ul>
@for (var i = 0; i < Model.Projects.Count; i++)
{
<li>
<input type="checkbox" asp-for="@Model.Projects[i].Selected" />
<label asp-for= "@Model.Projects[i].Selected"> @Model.Projects[i].Name</label>
<input type="hidden" asp-for="@Model.Projects[i].Value" />
<input type="hidden" asp-for="@Model.Project[i].Text" />
</li>
}
</ul>