<select> 下拉列表无法使用 ASP.Net Core MVC 6 从视图模型中识别 属性
<select> dropdown is not recognizing the property from the ViewModel in view using ASP.Net Core MVC 6
我正在研究 ASP.NET Core 6 MVC。在视图中,我创建了一个带有标签的下拉列表。
我正在尝试按照 Microsoft 文档中的教程从 ViewModel 绑定此下拉列表。我想我做的一切都很好,但没有得到结果。
当我尝试将 属性 与 属性 绑定时,无法识别 属性。请看下面的代码。
//视图:参见 select 的代码。无法识别 SelectedValue 和 Model.ddAircraft。
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
<div class="form-group row">
<label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Aircraft</label>
<div class="col-sm-3">
<select asp-for="SelectedValue" asp-items="Model.ddAircrafts"></select>
</div>
</div>
<div class="form-group row">
<label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Disk #</label>
<div class="col-sm-4">
<input type="number" class="" name="txtDisk" id="txtDisk" placeholder="">
</div>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DiskNumber)
</th>
'<th>
@Html.DisplayNameFor(model => model.PartID)
</th>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.DiskNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.PartID)
</td>
</tr>
}
</tbody>
</table>
//动作方法:
public IActionResult Index()
{
var data= _aircraftBu.getAircraft();
List<SelectListItem> dropdown = new List<SelectListItem>();
foreach(var a in data)
{
var listItem = new SelectListItem();
listItem.Text = a.Registration;
listItem.Value = a.Registration;
dropdown.Add(listItem);
}
dropdown.Insert(0, new SelectListItem { Text = "0", Value = "Select Item" });
var listPPVM = new List<PlannedPartInstallationViewModel>();
var plannedPartInstallation = new PlannedPartInstallationViewModel()
{
ddAircrafts = dropdown,
DiskNumber = "33342",
PartID = 12343,
SelectedValue = "Select Item"
};
listPPVM.Add(plannedPartInstallation);
return View(listPPVM);
}
/视图模型
public class PlannedPartInstallationViewModel
{
public int PartID { get; set; }
public string DiskNumber { get; set; }
public string SelectedValue { get; set; }
public List<SelectListItem> ddAircrafts { get; set; }
}
}
这是 link 我关注的 Select 标签助手。
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-6.0
实际上,您不必将下拉列表写入您的模型,您只需Viewbag
传递下拉列表的项目即可。参考这个简单的demo。
public class PlannedPartInstallationViewModel
{
public int PartID { get; set; }
public string DiskNumber { get; set; }
public string SelectedValue { get; set; }
}
控制器
public IActionResult Index()
{
var data= _aircraftBu.getAircraft();
List<SelectListItem> dropdown = new List<SelectListItem>();
foreach(var a in data)
{
var listItem = new SelectListItem();
listItem.Text = a.Registration;
listItem.Value = a.Registration;
dropdown.Add(listItem);
}
dropdown.Insert(0, new SelectListItem { Text = "0", Value = "Select Item" });
//use viewbag to pass the data of dropdownlist form controller to view
ViewBag.item= dropdown;
//.......
return View(listPPVM);
}
查看
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
//........
<select asp-for="SelectedValue" asp-items="@ViewBag.item"></select>
//.......
编辑=====================================
因为你传递的是list对象,所以不能直接用asp-for=Model.property
绑定
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
<select name="SelectedValue" >
@foreach (var item in @Model )
{
@foreach(var option in item.ddAircrafts)
{
<option value="@option.Value">@option.Text</option>
}
}
</select>
我正在研究 ASP.NET Core 6 MVC。在视图中,我创建了一个带有标签的下拉列表。
我正在尝试按照 Microsoft 文档中的教程从 ViewModel 绑定此下拉列表。我想我做的一切都很好,但没有得到结果。
当我尝试将 属性 与 属性 绑定时,无法识别 属性。请看下面的代码。
//视图:参见 select 的代码。无法识别 SelectedValue 和 Model.ddAircraft。
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
<div class="form-group row">
<label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Aircraft</label>
<div class="col-sm-3">
<select asp-for="SelectedValue" asp-items="Model.ddAircrafts"></select>
</div>
</div>
<div class="form-group row">
<label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Disk #</label>
<div class="col-sm-4">
<input type="number" class="" name="txtDisk" id="txtDisk" placeholder="">
</div>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DiskNumber)
</th>
'<th>
@Html.DisplayNameFor(model => model.PartID)
</th>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.DiskNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.PartID)
</td>
</tr>
}
</tbody>
</table>
//动作方法:
public IActionResult Index()
{
var data= _aircraftBu.getAircraft();
List<SelectListItem> dropdown = new List<SelectListItem>();
foreach(var a in data)
{
var listItem = new SelectListItem();
listItem.Text = a.Registration;
listItem.Value = a.Registration;
dropdown.Add(listItem);
}
dropdown.Insert(0, new SelectListItem { Text = "0", Value = "Select Item" });
var listPPVM = new List<PlannedPartInstallationViewModel>();
var plannedPartInstallation = new PlannedPartInstallationViewModel()
{
ddAircrafts = dropdown,
DiskNumber = "33342",
PartID = 12343,
SelectedValue = "Select Item"
};
listPPVM.Add(plannedPartInstallation);
return View(listPPVM);
}
/视图模型
public class PlannedPartInstallationViewModel
{
public int PartID { get; set; }
public string DiskNumber { get; set; }
public string SelectedValue { get; set; }
public List<SelectListItem> ddAircrafts { get; set; }
}
}
这是 link 我关注的 Select 标签助手。
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-6.0
实际上,您不必将下拉列表写入您的模型,您只需Viewbag
传递下拉列表的项目即可。参考这个简单的demo。
public class PlannedPartInstallationViewModel
{
public int PartID { get; set; }
public string DiskNumber { get; set; }
public string SelectedValue { get; set; }
}
控制器
public IActionResult Index()
{
var data= _aircraftBu.getAircraft();
List<SelectListItem> dropdown = new List<SelectListItem>();
foreach(var a in data)
{
var listItem = new SelectListItem();
listItem.Text = a.Registration;
listItem.Value = a.Registration;
dropdown.Add(listItem);
}
dropdown.Insert(0, new SelectListItem { Text = "0", Value = "Select Item" });
//use viewbag to pass the data of dropdownlist form controller to view
ViewBag.item= dropdown;
//.......
return View(listPPVM);
}
查看
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
//........
<select asp-for="SelectedValue" asp-items="@ViewBag.item"></select>
//.......
编辑=====================================
因为你传递的是list对象,所以不能直接用asp-for=Model.property
绑定
@model IEnumerable<Lsap.Models.PlannedPartInstallationViewModel>
<select name="SelectedValue" >
@foreach (var item in @Model )
{
@foreach(var option in item.ddAircrafts)
{
<option value="@option.Value">@option.Text</option>
}
}
</select>