亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Javascript 將下拉列表添加到 MVC 中的動態行并將值綁定到模型

如何使用 Javascript 將下拉列表添加到 MVC 中的動態行并將值綁定到模型

瀟瀟雨雨 2022-06-16 17:23:05
我想在 javascript 中創建的行中添加一個下拉列表,并將所選下拉項的 id 或文本與我的模型綁定。這是表格:  <table id="LineItems" class="table table-bordered fixed">        <thead>                           <tr>                <th width="10%" class="text-center">                    @Html.Label("Qty")                </th>                <th width="40%" class="text-center">                    @Html.Label("Description")                </th>                <th width="15%" class="text-center">                    @Html.Label("Project Number")                </th>                <th width="10%" class="text-center">                    @Html.Label("Unit Cost")                </th>                @if (Model.CanCreateCustSignOff == true)                {                    <th width="10%" class="text-center">                        @Html.Label("Custom Sign off")                    </th>                    <th width="20%" class="text-center">                        @Html.Label("Approver")                    </th>                }                <th width="10%" class="text-center">                    @Html.Label("Action")                </th>            </tr>        </thead>        <tbody id="lineItems">        </tbody>    </table>    <div>        <input type="button" value="Add Line Item" class="btn btn-primary" onclick="addLineItem()" />    </div>查看模型 - 選擇列表項由控制器填充,由文本 = 用戶名和作為 Guid 的值組成。這些數據來自數據庫,我不知道會添加多少用戶。public List<string> Approvers { get; set; }public class LineItems{   public int Qty { get; set; }   public string Description { get; set; }   public decimal UnitCost { get; set; }   public decimal LineItemCost { get; set; }   public string ProjectNumber { get; set; }   public bool CustSignOff { get; set; }   public string ApproverId { get; set; }   public Guid? ProjectId { get; set; } }Javascript - 我已經能夠使用下面的方法成功綁定使用我的模型創建的所有行的條目。
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊


我可以通過創建行然后將選項添加到 ID 來解決這個問題:


 approverList = JSON.parse('@Html.Raw(Json.Encode(@Model.Approvers))');

            var ddlId = document.getElementById(ddlApproverId);

            for (index in approverList) {

                ddlId.options[ddlId.options.length] = new Option(approverList[index], index.value);

            };


查看完整回答
反對 回復 2022-06-16
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號