简体   繁体   English

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

[英]How to set default value in dropdownlistfor using ViewBag

I have seen other questions of same Title being asked before. 我之前曾问过其他相同标题的问题。 I've tried them, but they are not working. 我已经尝试过了,但是它们没有用。

I have a main list which I'm fetching like this: 我有一个主要列表,我这样获取:

ViewBag.ProgramListChecker = manager.GetProgramListForCommittee(RegistrationId);

GetProgramListForCommittee method is populating as: 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;
        }

And another list I'm using for dropdown values: 我正在使用另一个列表作为下拉值:

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

Method for this is :- 方法是:

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));
    }

I want to have default value in the dropdown from ViewBag.ProgramListChecker with list populated from ViewBag.InspectionActionList . 我想在从下拉菜单中默认值ViewBag.ProgramListChecker与填充的列表ViewBag.InspectionActionList Although the list is populating but default value is not selecting. 尽管列表正在填充,但未选择默认值。

This is my cshtml: 这是我的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>

First ensure that item.CommitteeActionId and the "DataValueField" in ViewBag.InspectionActionList are the same type and then use different name for the DropDownList as follows: 首先确保item.CommitteeActionId"DataValueField"ViewBag.InspectionActionList属于同一类型,然后使用不同名称DropDownList如下:

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

Here I have used ComActionId for example. 在这里,我以ComActionId为例。 Now it should work. 现在应该可以了。

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

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