【发布时间】:2019-09-26 14:27:23
【问题描述】:
我有一个相当大的表单,用于提交访问、保险信息和程序信息。如果他们当前不存在,我需要能够离开表格以创建新的患者、医生、保险等。当我被重定向回表单时,我需要保留表单数据以及添加的任何新数据。目前我正在使用 TempData 将每个属性的值保存在我的模型中。然后我参考 TempData 值来填充我的输入中的“值”字段。这适用于我的所有领域,包括文本和日期时间,但下拉菜单给我带来了问题。我正在使用 viewbag 来填充 DropDownListFor,最接近的实现是用我的 TempData(默认值)替换 DropDownListFor 中的第三个参数,并用我的 TempData 值设置一个隐藏字段。这远非坚如磐石,而且非常有问题。我希望下拉列表的行为类似于我的文本和日期时间输入,但是 DropDownListFor 没有“值”属性。有谁知道我如何在 DropDownListFor 的上下文中实现文本输入中的“价值”等价物? 这是我认为的 DropDownListFor
<div class="form-group">
<label asp-for="InsId" class="control-label">Insurance</label>
<a style="padding-left:18px;">
<button asp-controller="Insurances" asp-action="IndexSV" asp-route-id="@ViewBag.NextVisitID" class="btn btn-primary fa fa-plus-circle" style="background-color:#16a085;"></button>
</a>
@if (TempData.Peek("InsId") != null)
{
var InsName = (string)ViewBag.InsIdName;
@Html.DropDownListFor(modelItem => modelItem.InsId, (IEnumerable<SelectListItem>)ViewBag.Insurances, InsName, new { style = "width:80%;" })
<input type="hidden" asp-for="InsId" class="form-control" value="@InsuranceId" />
<span asp-validation-for="InsId" class="text-danger"></span>
}
else
{
@Html.DropDownListFor(modelItem => modelItem.InsId, (IEnumerable<SelectListItem>)ViewBag.Insurances, "--SELECT--", new { style = "width:80%;" })
<span asp-validation-for="InsId" class="text-danger"></span>
}
</div>
这就是我在控制器中获取数据的方式
ViewBag.Insurances = _context.Insurance.Select(p => new SelectListItem { Value = p.InsId.ToString(), Text = p.InsCarrierName }).ToList().OrderBy(x => x.Text);
【问题讨论】:
标签: c# .net asp.net-mvc asp.net-core