在一切都看我的模型之前。
系统部件模型
public class systemPart
{
public int id { get; set; }
public string systemName { get; set; }
public int systemLevel { get; set; }
}
我想用这个模型
Create
查看。也在
create
视图我想有一个
<select>
来自同一模型的TagHelper。
看
创造
视图
@model MyProject.Models.systemPart
<form asp-controller="SystemPart" asp-action="Create" method="post">
<div class="form-group">
<input asp-for="systemName" class="form-control" />
</div>
<div class="form-group">
<select asp-for="systemName" asp-items="@(new SelectList(ViewBag.SysList,"id","systemName"))" class="form-control"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</form>
在控制器中,我有:
public class RoleController : Controller
{
private readonly ApplicationDbContext _context;
public RoleController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public IActionResult Create()
{
var sp = new List<systemPart>();
sp = _context.sysPart_Tbl.ToList();
ViewBag.SysList = sp;
return View();
}
[HttpPost]
public IActionResult Create(systemPart model)
{
if (ModelState.IsValid)
{
//
}
return View(model);
}
}
午餐后项目
DropDownList
显示信息。但在选择一个选项后
下拉框
并且提交表单,我无法获得选中的文本和ID。
AT
HttpPost
操作方法,我要选定值的数量(选定
id
指派给
SystemLevel