c# - 选择 Tag Helper 混淆
问题描述
如果我有这个
public class EventListViewModel
{
public string Id { get; set; }
public string Description { get; set; }
}
并在控制器中
public IActionResult Index()
{
List<EventListViewModel> eventList = this.eventStructureBLL.EventListGetBy("it");
return View(eventList);
}
并在查看此
@model List<Common.DAL.ViewModels.EventListViewModel>
什么是正确的选择标签助手
<select asp-for="??" asp-items="???"></select>
解决方案
选择某些东西并将其发回需要将其绑定到的东西。由于它将是单个项目,因此您需要以下内容:
public class FooViewModel
{
public int SelectedEventId { get; set; }
}
那么,该属性就是您要绑定的:
<select asp-for="SelectedEventId" ...></select>
然后,您还需要传递您的选项列表,它应该是一个IEnumerable<SelectListItem>
. 由于您已经有一个视图模型,因此对于您要绑定的属性,请将其添加为那里的属性:
public class FooViewModel
{
public int SelectedEventId { get; set; }
public IEnumerable<SelectListItem> EventOptions { get; set; }
}
要在您的操作中填写此内容:
model.EventOptions = this.eventStructureBLL.EventListGetBy("it").Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Description
});
然后,当然,这个模型将是你传递给你的视图的东西:
return View(model);
并且,在视图中:
@model FooViewModel
最后,您的选择标签将是:
<select asp-for="SelectedEventId" asp-items="@Model.EventOptions"></select>
推荐阅读
- ios - 将实时照片转换为静止图像
- javascript - 使用 JavaScript 更改数组中嵌套对象的键
- javascript - 在 javascript/svg 中绘制一个带有两侧的框作为 Arc
- python - “UnboundLocalError:分配前引用的局部变量”使用全局变量后
- c# - EF 5 Core 在scaffold-dbcontext 中添加类名前缀
- javascript - 带有 setTimeout() 函数的动画和过渡属性
- sql - 统计SQL中连续结果的个数
- java - 在上一个测试用例打开的页面上工作时出现TestNG Null Pointer Exception错误
- pipeline - 有没有办法让参数的可能值成为另一个存储库的分支
- c# - C# linq join 返回多个结果