Asp.net core 如何绑定数据并获取select TagHelper 的值?
Asp.net core How to bind data and get value of select TagHelper?
一切先看我的模型
系统部件模型
public class systemPart
{
public int id { get; set; }
public string systemName { get; set; }
public int systemLevel { get; set; }
}
我想在 Create
视图中使用此模型。同样在 create
视图中,我想要一个来自同一模型的 <select>
TagHelper。
查看Create
查看
@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>
在 Controller 中我有:
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
显示信息。但是在 select DropDownList
中的一个选项并提交表单后,我无法获得 selected 文本和 id。
在 HttpPost
操作方法中,我希望将 selected 值 (selected id
) 的数量分配给 SystemLevel
将 asp-for="systemName" 更改为 asp-for="SystemLevel"!
<div class="form-group">
<select asp-for="SystemLevel" asp-items="@(new SelectList(ViewBag.SysList,"id","systemName"))" class="form-control"></select>
</div>
一切先看我的模型
系统部件模型
public class systemPart
{
public int id { get; set; }
public string systemName { get; set; }
public int systemLevel { get; set; }
}
我想在 Create
视图中使用此模型。同样在 create
视图中,我想要一个来自同一模型的 <select>
TagHelper。
查看Create
查看
@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>
在 Controller 中我有:
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
显示信息。但是在 select DropDownList
中的一个选项并提交表单后,我无法获得 selected 文本和 id。
在 HttpPost
操作方法中,我希望将 selected 值 (selected id
) 的数量分配给 SystemLevel
将 asp-for="systemName" 更改为 asp-for="SystemLevel"!
<div class="form-group">
<select asp-for="SystemLevel" asp-items="@(new SelectList(ViewBag.SysList,"id","systemName"))" class="form-control"></select>
</div>