代码之家  ›  专栏  ›  技术社区  ›  Anjali

下拉列表显示Microsoft。AspNetCore。Mvc。翻译选择listItem而不是实际文本值

  •  0
  • Anjali  · 技术社区  · 1 年前

    我的Razor页面上有一个下拉列表。我正在将下拉列表与ViewData绑定。以下是我从数据库中获取数据的控制器的代码:

    public async Task<IActionResult> Index()
    {
        List<DocumentType> docTypes = new List<DocumentType>();
        docTypes = await _documentTypeService.GetDocumentTypes(deptId);
    
        ViewData["DocumentType"] = new SelectList(docTypes, "Id", "Description");
    }   
    

    这就是我显示下拉列表的方式,我还根据特定条件为下拉列表选择一个值。

    这是视图标记:

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Type</th>
        </thead>
        <tbody>     
        @if (Model != null)
        {
            @foreach (var item in Model)
            {
                string selected = String.Empty;
                  
                @foreach (var code in (SelectList)ViewData["DocumentType"])
                {
                     if (code.Text.Contains("This is the test for RCA-500"))
                     {
                           selected = code.Text;
                           break;
                     }
                }
                <tr>
                    <td>
                        <select class="form-control" style="min-width:150px" id="[email protected]" asp-items="@(new SelectList(ViewBag.DocumentType,selected))" asp-for="@item.DocumentType"></select>
                    </td>
                </tr>
    

    此下拉菜单显示的是“Microsoft.AspNetCore.Mvc.Frendering.SelectlistItem”,而不是实际值。不确定我做错了什么。

    我试着移除 @ 符号,但随后我开始得到一个编译器错误,说缺少“;”。

    1 回复  |  直到 1 年前
        1
  •  2
  •   Yong Shun    1 年前

    您必须提供所显示文本的属性名称及其值,如下所示。

    <select class="form-control" style="min-width:150px" id="[email protected]" 
        asp-items="@(new SelectList(ViewBag.DocumentType, "Value", "Text", selected))" 
        asp-for="@item.DocumentType">
    </select>
    
    

    但是,我相信上面的方法不会以编程方式选择正确的选项,因为它将基于 DocumentType 的值。

    您应该将所需的选定选项设置为 文档类型 而不是使用 selected 变量

    @foreach (var code in (SelectList)ViewData["DocumentType"])
    {
        if (code.Text.Contains("This is the test for RCA-500"))
        {
            item.DocumentType = Convert.ToInt32(code.Value);  // Convert the `Value` to the `DocumentType` type
            break;
        }
    }
    
    <tr>
        <td>
            <select class="form-control" style="min-width:150px" id="[email protected]" 
                asp-items="@(ViewBag.DocumentType as SelectList)" 
                asp-for="@item.DocumentType">
            </select>
        </td>
    </tr>
    

    没有 foreach 循环,您可以使用实现 First() 来自 系统林克 .

    @using System.Linq;  // Place on the top of the page
    
    @foreach (var item in Model)
    {
        item.DocumentType = Convert.ToInt32(((SelectList)ViewData["DocumentType"]).First(x => x.Text.Contains("This is the test for RCA-500")).Value);
    
        <tr>
            <td>
                <select class="form-control" style="min-width:150px" id="[email protected]" 
                    asp-items="@(ViewBag.DocumentType as SelectList)" 
                    asp-for="@item.DocumentType">
                </select>
            </td>
        </tr>
    }
    

    并且上述逻辑可以在控制器动作中完成。