【发布时间】:2021-05-14 11:58:00
【问题描述】:
我是 MVC 和 ASP.NETCore 的新手。我正在尝试创建一个页面,用户可以在其中将下拉列表中的员工 ID 分配给工单。一切看起来都很好,我在视图中获得了数据,但是当我按下分配按钮时,我没有在 HTTP Post 的模型中获得任何数据。
视图模型
namespace AVI_IT.ViewModels
{ 公共类 AdminPageViewModel {
public Dictionary<int, List<SelectListItem>> dict { get; set; }
}
}
控制器
public class AdminPageController : Controller
{
private readonly AvivDataContext _db;
public AdminPageController(AvivDataContext db)
{
this._db = db;
}
public IActionResult Index()
{
AdminPageViewModel thisViewModel = new AdminPageViewModel();
var employeeList = new List<SelectListItem>();
var dict = new Dictionary<int, List<SelectListItem>>();
foreach (Employee employee in _db.employee)
{
SelectListItem item = new SelectListItem();
item.Value = employee.employeeID.ToString();
item.Text = employee.employeeID.ToString();
employeeList.Add(item);
}
foreach (HelpDeskViewModel helpDesk in _db.helpdesk)
{
if (helpDesk.empID == null)
{
thisViewModel.dict = dict;
if(!dict.ContainsKey(helpDesk.ticketID))
thisViewModel.dict.Add(helpDesk.ticketID, employeeList);
}
}
return View(thisViewModel);
}
[HttpPost]
public IActionResult Index(AdminPageViewModel thisViewModel)
{
if (!ModelState.IsValid)
{
return View(thisViewModel);
}
return RedirectToAction("Index");
}
查看
@model AVI_IT.ViewModels.AdminPageViewModel
<fieldset>
@using (Html.BeginForm())
{
<form method="post">
<table align="center">
<tr>
<td>
<button class=" btn btn-primary">@Html.ActionLink("Create Role", "CreateRole",
"Administration")</button>
</td>
<td>
<button class=" btn btn-primary" asp-action="Index" asp-controller="UserRoles">Manage
Roles</button>
</td>
</tr>
</table>
<table class="table table-striped">
<thead>
<tr>
<th>Ticket Number</th>
<th>Assign Employee</th>
</tr>
</thead>
@foreach (var ticket in Model.dict)
{
<tr>
<td>@ticket.Key</td>
<td>@Html.DropDownListFor(Model => ticket.Value, new SelectList(ticket.Value,
"Value", "Text"), "Assign Employee", new { @class = "form-control" })</td>
</tr>
}
<tr><td><button type="submit" class="btn btn-primary form-control">Assign</button></td></tr>
</table>
</form>
}
</fieldset>
我被困在这里,任何帮助将不胜感激。谢谢
【问题讨论】:
标签: model-view-controller asp.net-core-mvc