如何使用ViewBag在下拉列表中设置默认值

时间:2019-03-10 09:14:36

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

我以前也曾问过其他相同标题的问题。我已经尝试过了,但是它们不起作用。

我有一个这样的主要列表:

ViewBag.ProgramListChecker = manager.GetProgramListForCommittee(RegistrationId);

GetProgramListForCommittee方法填充为:

public List<ProgramListCheckerEntity> GetProgramListForCommittee(int RegistrationId)
    {
        try
        {
            List<ProgramListCheckerEntity> list = new List<ProgramListCheckerEntity>();
            DataTable dt = _repository.GetProgramListForCommittee(RegistrationId);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ProgramListCheckerEntity obj = new ProgramListCheckerEntity();
                    obj.ItemName = dr.IsNull("ItemName") ? "" : dr["ItemName"].ToString();
                    obj.ResultOption = dr.IsNull("SelectedOptionName") ? "" : dr["SelectedOptionName"].ToString();
                    obj.Remarks = dr.IsNull("Remarks") ? "" : dr["Remarks"].ToString();
                    obj.CheckListRowId = dr.IsNull("CheckListRowId") ? 0 : Convert.ToInt32(dr["CheckListRowId"]);
                    obj.CommitteeActionId = dr.IsNull("CommitteeActionId") ? 0 : Convert.ToInt32(dr["CommitteeActionId"]);
                    obj.CommitteeRemarks = dr.IsNull("CommitteeRemarks") ? "" : dr["CommitteeRemarks"].ToString();
                    list.Add(obj);
                }
            }

            return list;
        }

还有另一个我用于下拉列表的列表:

 ViewBag.InspectionActionList = obj.GetMasterTableList("Master.InspectionAction", "ActionId", "ActionName", "");

此方法是:-

public List<DropDownEntity> GetMasterTableList(string TableName, string DataValueField, string DataTextField)
    {
        var parms = new Dictionary<string, object>();
        var spName = ConfigurationManager.AppSettings["GetMasterTableList"];

        parms.Add("@TableName", TableName);
        parms.Add("@DataValueField", DataValueField);
        parms.Add("@DataTextField", DataTextField);

        return DataTableToList(_dbAccess.GetDataTable(spName, parms));
    }

我希望在ViewBag.ProgramListChecker的下拉列表中具有默认值,并从ViewBag.InspectionActionList填充列表。尽管列表正在填充,但默认值未选择。

这是我的cshtml:

<table id="tblList" class="table dva-table dva-tborder agencygrid">
    <tbody>
        @{int counter = 0; }

        @foreach (var item in ViewBag.ProgramListChecker)
        {
            <tr>
                <td style="width:5%;text-align:center">
                    @(++counter)
                </td>
                <td style="width:25%;text-align:left">
                    @item.ItemName
                </td>
                <td style="width:10%;text-align:center">
                    @item.ResultOption
                </td>
                <td style="width:25%;text-align:center">
                    @item.Remarks                                    
                </td>
                <td style="width:15%;text-align:center">
                    @Html.DropDownListFor(m => m.CommitteeActionId, new SelectList(ViewBag.InspectionActionList, "DataValueField", "DataTextField", item.CommitteeActionId), new
               {
                   @id = "ddlAction",
                   @class = "form-control requiredValidation"
               })
                </td>
                <td style="width:35%;text-align:center">
                    <input type="text" id="CommitteeRemarks" value="@item.CommitteeRemarks" maxlength="1000" rows="10" class="form-control" />
                    <input type="hidden" id="CheckListRowId" value="@item.CheckListRowId" class="form-control" />
                </td>
            </tr>
        }
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

首先确保item.CommitteeActionId中的"DataValueField"ViewBag.InspectionActionList是同一类型,然后为DropDownList使用不同的名称,如下所示:

@Html.DropDownList("ComActionId", new SelectList(ViewBag.InspectionActionList, "DataValueField", "DataTextField", item.CommitteeActionId), new
               {
                   @id = "ddlAction",
                   @class = "form-control requiredValidation"
               })

例如,我在这里使用了ComActionId。现在应该可以了。