ASP.NET Core MVC 前端开发 List show View keep getting System.Collections.Generic.List
ASP.NET Core MVC frontend development List show View keep getting System.Collections.Generic.List
我正在尝试显示来自三个模型的数据,有两个模型(STAFF、DEPARTMENT)使用显示数据,制作级联下拉列表
一个模型(STAFF_PERMISSION)插入数据,使
但不断得到:The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[Voyager.Models.Department.Staff_To_Show]
StaffPermission.cshtml
@model Voyager.Models.Department.Staff_To_Show
@{
ViewBag.Title = "new";
}
<h2>new</h2>
<form method="post" action="/Home/StaffPermission">
@Html.AntiForgeryToken()
<div class="form-horizontal">
<select id="DepartmentNumber" name="DepartmentNumber"class="form-control">
<option value="">select</option>
@foreach (var item in Model.Departments)
{
<option value="@item.DepartmentNumber"> @Html.DisplayFor(modelItem => item.DepartmentNumber)</option>
}
</select>
<br/>
<select id="DepartmentNumber" name="DepartmentNumber"class="form-control">
<option value="">select</option>
@foreach (var item in Model.Staffs)
{
<option value="@item.StaffNumber"> @Html.DisplayFor(modelItem => item.StaffNumber)</option>
}
</select>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.StaffPermissions, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StaffPermissions, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StaffPermissions, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="add" class="btn btn-success" />
</div>
</div>
</div>
</form>
HomeController.cs
[HttpGet("[action]")]
public ActionResult StaffPermission()
{
var query = dbContext.staff.ToList();
var queryDepartment = dbContext.Departments.ToList();
var model = new List<Staff_To_Show>();
List<Department> dataDepartment = new List<Department>();
dataDepartment.AddRange(queryDepartment);
List<staff> datastaff = new List<staff>();
datastaff.AddRange(query);
model.Add(new Staff_To_Show() { Departments = dataDepartment });
model.Add(new Staff_To_Show() { Staffs = datastaff });
return View(model);
}
[HttpPost]
public ActionResult StaffPermission(String DepartmentNumber, String StaffNumber, string AesUpd)
{
if (string.IsNullOrWhiteSpace(DepartmentNumber))
{
ModelState.AddModelError("Department", "error");
return View();
}
else
{
string[] password = getTool.Create(AesUpd);
StaffPermission addstaff = new StaffPermission();
addstaff.AesUpd = password[0];
addstaff.AesKey = password[1];
addstaff.AesIv = password[2];
addstaff.LastChangePasswordTime = DateTime.Now;
addstaff.CreateDate = DateTime.Now;
addstaff.LastUpd = password[0];
addstaff.LastKey = password[1];
addstaff.LastIv = password[2];
addstaff.IsEnabled = true;
addstaff.StaffNumber = StaffNumber;
addstaff.DepartmentNumber = Convert.ToInt16(DepartmentNumber);
dbContext.StaffPermissions.Add(addstaff);
dbContext.SaveChanges();
return View();
}
}
StaffToShow.cs
public partial class Staff_To_Show
{
public List<Department> Departments { get; set; } = null!;
public List<staff> Staffs { get; set; } = null!;
public StaffPermission StaffPermissions { get; set; } = null!;
}
StaffPermission.cs
using System;
using System.Collections.Generic;
namespace Voyager.Models.Department
{
public partial class StaffPermission
{
public int DepartmentNumber { get; set; }
public string StaffNumber { get; set; } = null!;
public string AesUpd { get; set; } = null!;
public string AesKey { get; set; } = null!;
public string AesIv { get; set; } = null!;
public DateTime? CreateDate { get; set; }
public string? LastUpd { get; set; }
public string? LastKey { get; set; }
public string? LastIv { get; set; }
public DateTime? LastChangePasswordTime { get; set; }
public bool? IsEnabled { get; set; }
public virtual staff StaffNumberNavigation { get; set; } = null!;
}
}
staff.cs
public partial class staff
{
public int DepartmentNumber { get; set; }
public string StaffNumber { get; set; } = null!;
public string? Name { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public string? Email { get; set; }
public bool? IsEnabled { get; set; }
public DateTime RecDate { get; set; }
public virtual Department DepartmentNumberNavigation { get; set; } = null!;
public virtual StaffPermission StaffPermission { get; set; } = null!;
}
Department.cs
public partial class Department
{
public Department()
{
staff = new HashSet<staff>();
}
public int DepartmentNumber { get; set; }
public string? DepartmentName { get; set; }
public virtual ICollection<staff> staff { get; set; }
}
如有任何帮助,我们将不胜感激
您的视图需要一个 Staff_To_Show
类型的模型,但在您的 HttpGet 方法 StaffPermission
中,您创建并传递了一个 List<Staff_To_Show>
类型的模型。
所以你的方法应该是这样的:
[HttpGet("[action]")]
public ActionResult StaffPermission()
{
var datastaff = dbContext.staff.ToList();
var dataDepartment = dbContext.Departments.ToList();
var model = new Staff_To_Show {
Departments = dataDepartment,
Staffs = datastaff
};
return View(model);
}
我还删除了多余的列表创建代码,因为部门和员工已经是列表。
我正在尝试显示来自三个模型的数据,有两个模型(STAFF、DEPARTMENT)使用显示数据,制作级联下拉列表 一个模型(STAFF_PERMISSION)插入数据,使
但不断得到:The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[Voyager.Models.Department.Staff_To_Show]
StaffPermission.cshtml
@model Voyager.Models.Department.Staff_To_Show
@{
ViewBag.Title = "new";
}
<h2>new</h2>
<form method="post" action="/Home/StaffPermission">
@Html.AntiForgeryToken()
<div class="form-horizontal">
<select id="DepartmentNumber" name="DepartmentNumber"class="form-control">
<option value="">select</option>
@foreach (var item in Model.Departments)
{
<option value="@item.DepartmentNumber"> @Html.DisplayFor(modelItem => item.DepartmentNumber)</option>
}
</select>
<br/>
<select id="DepartmentNumber" name="DepartmentNumber"class="form-control">
<option value="">select</option>
@foreach (var item in Model.Staffs)
{
<option value="@item.StaffNumber"> @Html.DisplayFor(modelItem => item.StaffNumber)</option>
}
</select>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.StaffPermissions, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StaffPermissions, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StaffPermissions, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="add" class="btn btn-success" />
</div>
</div>
</div>
</form>
HomeController.cs
[HttpGet("[action]")]
public ActionResult StaffPermission()
{
var query = dbContext.staff.ToList();
var queryDepartment = dbContext.Departments.ToList();
var model = new List<Staff_To_Show>();
List<Department> dataDepartment = new List<Department>();
dataDepartment.AddRange(queryDepartment);
List<staff> datastaff = new List<staff>();
datastaff.AddRange(query);
model.Add(new Staff_To_Show() { Departments = dataDepartment });
model.Add(new Staff_To_Show() { Staffs = datastaff });
return View(model);
}
[HttpPost]
public ActionResult StaffPermission(String DepartmentNumber, String StaffNumber, string AesUpd)
{
if (string.IsNullOrWhiteSpace(DepartmentNumber))
{
ModelState.AddModelError("Department", "error");
return View();
}
else
{
string[] password = getTool.Create(AesUpd);
StaffPermission addstaff = new StaffPermission();
addstaff.AesUpd = password[0];
addstaff.AesKey = password[1];
addstaff.AesIv = password[2];
addstaff.LastChangePasswordTime = DateTime.Now;
addstaff.CreateDate = DateTime.Now;
addstaff.LastUpd = password[0];
addstaff.LastKey = password[1];
addstaff.LastIv = password[2];
addstaff.IsEnabled = true;
addstaff.StaffNumber = StaffNumber;
addstaff.DepartmentNumber = Convert.ToInt16(DepartmentNumber);
dbContext.StaffPermissions.Add(addstaff);
dbContext.SaveChanges();
return View();
}
}
StaffToShow.cs
public partial class Staff_To_Show
{
public List<Department> Departments { get; set; } = null!;
public List<staff> Staffs { get; set; } = null!;
public StaffPermission StaffPermissions { get; set; } = null!;
}
StaffPermission.cs
using System;
using System.Collections.Generic;
namespace Voyager.Models.Department
{
public partial class StaffPermission
{
public int DepartmentNumber { get; set; }
public string StaffNumber { get; set; } = null!;
public string AesUpd { get; set; } = null!;
public string AesKey { get; set; } = null!;
public string AesIv { get; set; } = null!;
public DateTime? CreateDate { get; set; }
public string? LastUpd { get; set; }
public string? LastKey { get; set; }
public string? LastIv { get; set; }
public DateTime? LastChangePasswordTime { get; set; }
public bool? IsEnabled { get; set; }
public virtual staff StaffNumberNavigation { get; set; } = null!;
}
}
staff.cs
public partial class staff
{
public int DepartmentNumber { get; set; }
public string StaffNumber { get; set; } = null!;
public string? Name { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public string? Email { get; set; }
public bool? IsEnabled { get; set; }
public DateTime RecDate { get; set; }
public virtual Department DepartmentNumberNavigation { get; set; } = null!;
public virtual StaffPermission StaffPermission { get; set; } = null!;
}
Department.cs
public partial class Department
{
public Department()
{
staff = new HashSet<staff>();
}
public int DepartmentNumber { get; set; }
public string? DepartmentName { get; set; }
public virtual ICollection<staff> staff { get; set; }
}
如有任何帮助,我们将不胜感激
您的视图需要一个 Staff_To_Show
类型的模型,但在您的 HttpGet 方法 StaffPermission
中,您创建并传递了一个 List<Staff_To_Show>
类型的模型。
所以你的方法应该是这样的:
[HttpGet("[action]")]
public ActionResult StaffPermission()
{
var datastaff = dbContext.staff.ToList();
var dataDepartment = dbContext.Departments.ToList();
var model = new Staff_To_Show {
Departments = dataDepartment,
Staffs = datastaff
};
return View(model);
}
我还删除了多余的列表创建代码,因为部门和员工已经是列表。