简体   繁体   English

如何动态地将下拉列表添加到 td

[英]How to add dropdownlist to a td dynamically

We have an old ASP.NET MVC framework and we are in the progress of moving to .NET 5 MVC The ViewModel that we receive from the controller contains several things but one of these is called ListOfDayTypes and it's a list of SelectListItem我们有一个旧的 ASP.NET MVC 框架,我们正在迁移到 .NET 5 MVC 我们从 controller 收到的 ViewModel 包含几个东西,但其中一个是 ListDaysType

We use html table that we render dynamically and each of these td looks the same except id,name and value我们使用动态渲染的 html 表,每个 td 看起来都一样,除了 id、name 和 value

<td width="29" id="17_2021-1-2">                            
   <input type="hidden" name="Schedule[0].Value[1].ScheduleId" value="0">
   <input type="hidden" name="Schedule[0].Value[1].Date" value="2021-1-2">
   <input type="hidden" name="Schedule[0].Value[1].DayTypeId" value="0">
   <input type="hidden" name="Schedule[0].Value[1].HasChanged" value="false">
   <input type="hidden" name="Schedule[0].Value[1].ShiftTypeId" value="1008">
   <input type="hidden" name="Schedule[0].Value[1].ShiftTeamId" value="17">
  <a class="defaultScheduleLink" onclick="javascript:showDayTypes($(this));"> * </a>
</td>

When we click an anchor in a td cell we call function showDayTypes($(this) because we must show a dropdownlist where we can select an item from. This works fine in the old ASP.NET framework but we have trouble in .NET 5 When we click an anchor in a td cell we call function showDayTypes($(this) because we must show a dropdownlist where we can select an item from. This works fine in the old ASP.NET framework but we have trouble in .NET 5

In the old code we have the following:在旧代码中,我们有以下内容:

<script language="javascript" type="text/javascript">
   var ddlHtml = '@Html.Raw(Html.DropDownList("ddlDayType", Model.ListOfDayTypes, new { id="ddlDayType", @class="scheduleDDL",        onchange="onDayTypeChanged();"} ).ToString().Replace(System.Environment.NewLine, ""))';
</script>

When clicking in an anchor in td cell this showDayTypes code is called As you can see we just use variable ddlHtml from @Html.Raw(...) to populate the dropdown list to it's parent which is a td and this works well.当单击 td 单元格中的锚点时,此 showDayTypes 代码被调用如您所见,我们只是使用来自@Html.Raw(...)的变量 ddlHtml 将下拉列表填充到它的父级,这是一个 td 并且效果很好。

function showDayTypes(link)
{
    var td = link.parent("td");
    link.remove(); 
    td.append(ddlHtml);
    var select = td.children("select");
    var option = select.children("option[text='" + link.text() + "']");
    select.val(option.val());
}

I have tried to use the same code in .NET 5 but I get Microsoft.AspNetCore.Mvc.Rendering.TagBuilder when I try to use @Html.Raw It seems to me they have removed some good functionality in .NET 5 which is bad.我尝试在 .NET 5 中使用相同的代码,但是当我尝试使用@Html.Raw时,我得到了Microsoft.AspNetCore.Mvc.Rendering.TagBuilder在我看来,他们已经删除了 .NET 5 中的一些好的功能,这很糟糕。

What is the best way to solve my problem so the dropdownlist is displayed when clicking an anchor in a td and can select an item from the dropdownlist as we could in the old ASP.NET framework解决我的问题的最佳方法是什么,以便在单击 td 中的锚点时显示下拉列表,并且可以像在旧的 ASP.NET 框架中一样从下拉列表中选择 select 项目

I have looked everywhere but no good solutions.我到处寻找,但没有好的解决方案。

I get the message back saying Microsoft.AspNetCore.Mvc.Rendering.TagBuilder我收到消息说 Microsoft.AspNetCore.Mvc.Rendering.TagBuilder

For this issue, you could change your code like below:对于这个问题,您可以更改您的代码,如下所示:

var ddlHtml = `@Html.DropDownList("ddlDayType", Model.ListOfDayTypes, new { id = "ddlDayType", @class = "scheduleDDL", onchange = "onDayTypeChanged();" })`;

Note : the symbol is ` not single quote(').注意:符号是`不是单引号(')。

Besides, your code seems also want to set link text as selected item.此外,您的代码似乎还想将链接文本设置为选定项。 If so, modify like below:如果是这样,修改如下:

<script>
    var ddlHtml = `@Html.DropDownList("ddlDayType", Model.ListOfDayTypes, new { id = "ddlDayType", @class = "scheduleDDL", onchange = "onDayTypeChanged();" })`;
    function showDayTypes(link) {
        var td = link.parent("td");
        link.remove();
        td.append(ddlHtml);
        var select = td.children("select");

        var option = select.children("option").filter(":contains('" + link.text()+"')");

        select.val(option.val());
    }
</script>

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM