如果我有这个
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>