How to show dropdownlist value on edit

有些话、适合烂在心里 提交于 2019-12-25 04:19:34

问题


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)%>

回答1:


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>


来源:https://stackoverflow.com/questions/3526894/how-to-show-dropdownlist-value-on-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!