如何显示在编辑的DropDownList值 [英] How to show dropdownlist value on edit

查看:154
本文介绍了如何显示在编辑的DropDownList值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题扩展这个问题

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

例如:在我的箱子DROPDOWNLIST我有A B C D项。

当我添加任何项目我显示网格,我重装我的网页。

我的网格有两列一个是附加价值DROPDOWNLIST。其他一些其他的文本值。

在我的网格每一行有编辑按钮。

当我点击编辑我重装我的网页编辑该选择的DropDownList值。

当我点击编辑我需要显示有史以来DROPDOWNLIST值我在我的网格需要什么在DropDownList显示..

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

请让我知道,如果任何机构没有undrestood我的问题。

感谢

我的Controler code ..

 公众的ActionResult编辑(INT?ID)
        {
            如果(id.HasValue)
            {
               // _viewModel.ObnCategoryTextComponent = _obnRepository.GetObnCategoryById(id.Value);
                 VAR数据= _obnRepository.GetSingle< ObnCategory>(id.Value);
                串了selectedValue = data.ObnCategoryName;
                _viewModel.ServiceTypeListAll =新的SelectList(_bvRepository.GetAllServiceTypes()的OrderBy(N =方式> n.ServiceTypeName),ServiceTypeName,ServiceTypeName了selectedValue);
                // _viewModel.Category = data.ObnCategoryName;
            }
            返回PartialView(指数,_视图模型);
        }

我的看法是..

 <%= Html.DropDownList(ServiceTypeListAll,Model.ServiceTypeListAll)%GT;


解决方案

您应该选择属性设置适当的项目为true:

 公众的ActionResult指数(INT ID)
{
    //串了selectedValue =textOfTheSelectedItem;
    串了selectedValue = _bvRepository.GetServiceType(ID)//我只能猜测,这将是您的存储库访问...
    _viewModel.ServiceTypeListAll =新的SelectList(_bvRepository.GetAllServiceTypes()了ToList()的OrderBy(N =方式>。n.ServiceTypeName).ToList(),ServiceTypeName,ServiceTypeName了selectedValue);
        返回查看(_viewModel);
}

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

对于下面的列表中,了selectedValue 必须是项目1或项目2:

 <选择>
    <期权价值=项目1>项目1 LT; /选项>
    <期权价值=项目2>项目2'; /选项>
< /选择>

this is my Question extension to this Question..

Here I am able to add the Dropdown list value to the Grid.. perfectly..

Ex: in my Dropdownlist box I have A B C D items..

When I add any Item I am displaying the grid and I am reloading my page.

My grid have two columns one is added Dropdownlist value.. other is some other text value..

each row in my grid have Edit button..

When I click Edit I am reloading my page to edit this selected dropdownlist value..

when I click Edit I need to show what ever the Dropdownlist value I have in the grid I need to show in the Dropdownlist..

so that user knows he has this dropdown value..

please let me know if any body not undrestood my question..

thanks

My Controler code..

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

My View is..

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

解决方案

You should set the appropriate items Selected property to 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 must match one of the ServiceTypeNames in the list:

For the following list, selectedValue must either be "Item 1" or "Item 2":

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

这篇关于如何显示在编辑的DropDownList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆