【问题标题】:How to show dropdownlist value on edit如何在编辑时显示下拉列表值
【发布时间】:2010-08-20 07:11:54
【问题描述】:

这是我对this Question..的问题扩展。

在这里我可以将下拉列表值添加到网格中.. 完美..

例如:在我的下拉列表框中,我有 A B C D 项目..

当我添加任何项目时,我正在显示网格并重新加载我的页面。

我的网格有两列,一列添加了 Dropdownlist 值..另一个是其他一些文本值..

我的网格中的每一行都有编辑按钮..

当我点击编辑时,我正在重新加载我的页面来编辑这个选定的下拉列表值..

当我单击“编辑”时,我需要在网格中显示我需要在下拉列表中显示的 Dropdownlist 值。..

让用户知道他有这个下拉值..

如果有任何机构没有解决我的问题,请告诉我..

谢谢

我的控制器代码..

public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
               // _viewModel.ObnCategoryTextComponent = _obnRepository.GetObnCategoryById(id.Value);
                 var data = _obnRepository.GetSingle<ObnCategory>(id.Value);
                string selectedValue = data.ObnCategoryName;
                _viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().OrderBy(n => n.ServiceTypeName), "ServiceTypeName", "ServiceTypeName", selectedValue);
                // _viewModel.Category = data.ObnCategoryName;
            }
            return PartialView("Index",_viewModel);
        }

我的观点是..

 <%=Html.DropDownList("ServiceTypeListAll",Model.ServiceTypeListAll)%>

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    您应该将相应的 items Selected 属性设置为 true:

    public ActionResult Index(int id)
    {
        //string selectedValue = "textOfTheSelectedItem";
        string selectedValue = _bvRepository.GetServiceType(id)  // I only guess, that would be your repository access...
        _viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().ToList().OrderBy(n => n.ServiceTypeName).ToList(), "ServiceTypeName", "ServiceTypeName", selectedValue);
            return View(_viewModel);
    }
    

    selectedValue 必须匹配列表中的 ServiceTypeNames 之一:

    对于以下列表,selectedValue 必须是“项目 1”或“项目 2”:

    <select>
        <option value="Item 1">Item 1</option>
        <option value="Item 2">Item 2</option>
    </select>
    

    【讨论】:

    • 是的,您不必更改视图。这是由 Html Helper 自动处理的
    • 我的下拉列表值没有改变任何东西..这是我要更新的代码..谢谢
    • 您是否尝试过简化 Html Helper 代码? &lt;%=Html.DropDownList("ServiceTypeListAll", Model.ServiceTypeListAll)%&gt;
    • 由于您从与实际列表不同的存储库加载selectedValue,是否有可能该列表不包含selectedValue
    • 是的,在调试时我确实看到了 SelectedValue 的值..它来自同一个存储库..不是不同的..谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2019-05-05
    相关资源
    最近更新 更多