首页 > 解决方案 > 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在下拉列表中获取名称?

结果如下:

结果

谢谢。

标签: c#asp.netasp.net-mvcasp.net-corerazor

解决方案


你的课必须简单。

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"))

推荐阅读