【问题标题】:Asp.net MVC DropDownListFor list of lists from modelAsp.net MVC DropDownListFor 来自模型的列表列表
【发布时间】:2020-10-23 11:26:04
【问题描述】:

我正在尝试在 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" />
        }

在下拉列表而不是应用程序中,我正在获取 GenericList.Object

我必须修改什么才能在下拉列表中获得Applications 名称?

结果如下:

谢谢。

【问题讨论】:

  • 你不能使用像public string Id { get; set; } public List&lt;string&gt; Applications { get; set; }//["app1","app2"]这样的模式选择列表接受一对值id:1,text:'my name'首先你必须修复你的对,你试图为一个Id推送多个Applications跨度>
  • 是的,我有多个应用程序名称属于单个 ID,我想在 dropdowlist 中显示..如何为一个 id 实现多个 Applications
  • 如果是这样,这意味着如果用户选择一个id,它将选择所有属于id:1的应用程序名称。如果我说的是正确的,您可以为应用程序名称创建一个逗号分隔的字符串,然后将其显示为一条记录id:1,text:'app1,app2,app3'

标签: c# asp.net asp.net-mvc asp.net-core razor


【解决方案1】:

在下拉列表而不是应用程序中,我正在获取 GenericList.Object。

我必须修改什么才能在下拉列表中获取应用程序名称?

public List&lt;string&gt; Applications { get; set; }//["app1","app2"]

您可以尝试使用指定的分隔符连接字符串项,如下所示。

@Html.DropDownListFor(m => m.ApplicationName, new SelectList(Model.ApplicationName.Select(a=>new { Id = a.Id, Applications = String.Join(", ", a.Applications.ToArray())}), "Id", "Applications"), "Select Application", new { @class = "textboxStyle", placeholder = "Select your application...", autofocus = "autofocus", autocomplete = "on", onkeypress = "btnFocusActivate(this, event)", id = "un" })

测试结果

更新:

可以在运行时编辑/附加带有 id 的随机数,结果就像每个应用程序一样作为单独的选项。

要实现上述要求,正如我在评论中提到的,您可以尝试在控制器操作中根据您的模型数据生成预期数据,然后将其传递给视图以填充您的下拉列表,如下所示。

var Applications_list = "";


foreach (var item in model.ApplicationName)
{
    Applications_list += String.Join(",", item.Applications.ToArray()) + ",";
}

ViewBag.AppList = Applications_list.TrimEnd(',').Split(",").Select((a, index) => new { Id = index, AppName = a });

HTML 代码

@Html.DropDownListFor(m => m.ApplicationName, new SelectList(ViewBag.AppList, "Id", "AppName"), "Select Application", new { @class = "textboxStyle", placeholder = "Select your application...", autofocus = "autofocus", autocomplete = "on", onkeypress = "btnFocusActivate(this, event)", id = "un" })

测试结果

【讨论】:

  • 可以在运行时编辑/附加带有 id 的随机数,结果就像每个应用程序一样作为单独的选项。
  • 你的模型LoginModel的ApplicationName是否只有一项?
  • 没有..但是寻找映射以使 id 在运行时从 id 和文本的组合中变得唯一,并在下拉菜单中显示选项。
  • 我建议您可以根据控制器操作中的模型数据重新生成预期的List&lt;SelectListItem&gt;,并通过 viewbag 等将其传递给视图,而不是在视图中编写和执行过多的逻辑代码。
  • it's possible to edit/append random number with id at runtime and result like each app as separate options. 嗨@r08,你可以按照我在更新中分享的方法来实现要求。
【解决方案2】:

你的课必须是简单的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多