c# - Asp.net MVC DropDownListFor 来自模型的列表列表
问题描述
我正在尝试在 Model 的下拉列表中显示名称,如下所示 Model.list<AnotherModel>.List<string>
。
模型
public class LoginModel
{
public List<ApplicationNames> ApplicationName { get; set; }
}
public class ApplicationNames
{
public string Id { get; set; }
public List<string> Applications { get; set; }//["app1","app2"]
}
CSHTML
@model LoginModel
@using (Html.BeginForm("ApplicationIndex", "Home", FormMethod.Get))
{
@Html.DropDownListFor(m => m.ApplicationName,new SelectList(Model.ApplicationName,"Id","Applications"),"Select Application", new { @class = "textboxStyle", placeholder = "Select your application...", autofocus = "autofocus", autocomplete = "on", onkeypress = "btnFocusActivate(this, event)", id = "un" })
<input type="submit" value="NEXT" class="rectangle" id="submitNext" disabled="disabled" />
}
在下拉列表中,而不是 Applications 我得到 Getting GenericList.Object
。
我必须修改什么才能Applications
在下拉列表中获取名称?
结果如下:
谢谢。
解决方案
你的课必须简单。
public class LoginModel
{
public string SelectedApplicationNameItemId { get; set; }
public List<ApplicationNameItem> ApplicationNames { get; set; }
}
public class ApplicationNameItem
{
public string Id { get; set; }
public string Name { get; set; }
}
Razor 页面的代码如下。提交时,选定的值可以位于SelectedApplicationNameItemId
属性中
Html.DropDownListFor(m => m.SelectedApplicationNameItemId, new SelectList(Model.ApplicationNames,"Id","Name"))
推荐阅读
- php - 如何使用 Amazon Polly 自动播放 MP3
- python - PySpark RDD mapPartitions 调用因异步代码而失败
- reactjs - 子导航器屏幕内的 React Native 调用导航器
- python - TemplateSyntaxError:预期标记“打印语句结束”,得到“r”
- django - Docker - Django - 在 Ubuntu 服务器上切换到主机卷后出现 PermissionError
- python - 从 one-hot 编码数据帧中获得胜率
- html - 如何使用 flexbox 在不移动周围元素的情况下扩展文本
- bash - 如何在 gitlab ci/cd 中使用自定义变量?
- amazon-web-services - 如何保持 S3 存储桶权限公开
- amazon-web-services - 通过快照 ID 创建的 Ebs 卷