[英]asp.net core dropdown list not setting selected value
I have a bootstrap drop down and I am using tag helpers to enter render values and read them on post.我有一个引导下拉菜单,我正在使用标签助手输入渲染值并在发布时读取它们。 Below is the code
下面是代码
asp-for="CfgType" -- value is set to SIN or UN based on that set the view to the selected value -- and on submittig the page I need to get the selected value for the drop down from CfgType property. asp-for="CfgType" -- 值设置为 SIN 或 UN 基于将视图设置为选定值 -- 并且在提交页面时,我需要从 CfgType 属性中获取下拉列表的选定值。
<div class="form-row">
<div class="col-md-12">
<div class="form-group form-group--float">
<div class="form-group form-group--float">
<select id="dwType" class="form-control custom-select" asp-for="CfgType">
@if(Model.VehFlg)
{
<option value="SIN">Serial Num</option>
}
@if(Model.VinFlg)
{
<option value="UN">Unit Number</option>
}
</select>
<label> Configuration Type</label>
<div class="invalid-tooltip"><span asp-validation-for="CfgType"></span></div>
</div>
<i class="form-group__bar"></i>
</div>
Below is how the value is being tried to retrive using controller下面是如何尝试使用 controller 检索该值
//now for fvIdentViewModel.CfgType is null //现在 fvIdentViewModel.CfgType 是 null
[Route("/fv-identification/Save"), HttpPost, ValidateAntiForgeryToken]
public IActionResult Save(FleetVehicleIdentificationViewModel fvIdentViewModel)
{
}
Here is a working demo:这是一个工作演示:
Model: Model:
public class FleetVehicleIdentificationViewModel
{
public string CfgType { get; set; }
public bool VehFlg { get; set; }
public bool VinFlg { get; set; }
}
View(Index.cshtml):查看(索引.cshtml):
@model FleetVehicleIdentificationViewModel
<form action="/fv-identification/Save" method="post">
@Html.AntiForgeryToken()
<div class="form-row">
<div class="col-md-12">
<div class="form-group form-group--float">
<div class="form-group form-group--float">
<select id="dwType" class="form-control custom-select" asp-for="CfgType">
@if (Model.VehFlg)
{
<option value="SIN">Serial Num</option>
}
@if (Model.VinFlg)
{
<option value="UN">Unit Number</option>
}
</select>
<label> Configuration Type</label>
<div class="invalid-tooltip"><span asp-validation-for="CfgType"></span></div>
</div>
<i class="form-group__bar"></i>
</div>
<div>
<input type="submit" value="create" />
</div>
</div>
</div>
</form>
Controller: Controller:
For easy testing,I set the value manually to render your Index.cshtml
:为了便于测试,我手动设置了值来呈现您的
Index.cshtml
:
public IActionResult Index()
{
var model = new FleetVehicleIdentificationViewModel()
{
VehFlg = true,
VinFlg = false
};
return View(model);
}
Your post action:您的帖子操作:
[Route("/fv-identification/Save"), HttpPost, ValidateAntiForgeryToken]
public IActionResult Save(FleetVehicleIdentificationViewModel fvIdentViewModel)
{
//do your stuff...
}
you should put in tag form with button type submit.您应该输入带有按钮类型提交的标签形式。
<form action="/fv-identification/Save" method="post">
<button type="submit" class="btn btn-info">@localizer["Submit"] </button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.