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.
<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
//now for fvIdentViewModel.CfgType is null
[Route("/fv-identification/Save"), HttpPost, ValidateAntiForgeryToken]
public IActionResult Save(FleetVehicleIdentificationViewModel fvIdentViewModel)
{
}
Here is a working demo:
Model:
public class FleetVehicleIdentificationViewModel
{
public string CfgType { get; set; }
public bool VehFlg { get; set; }
public bool VinFlg { get; set; }
}
View(Index.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:
For easy testing,I set the value manually to render your 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.