views:

31

answers:

1

Hello Friends,

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)%>
+2  A: 

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>
Dave
So what about my View? is that still same? thanks
Yes, you don't have to change the view. This is handled by the Html Helper automatically
Dave
My dropdown list value not changing anything.. Here is my code I will update.. thanks
Did you try to simplify the Html Helper code? `<%=Html.DropDownList("ServiceTypeListAll", Model.ServiceTypeListAll)%>`
Dave
Yes above is my Update Questions Dave..thanks
Since your loading `selectedValue` from a different repository than the actual list, is it possible, that the list doesn't contain `selectedValue`?
Dave
Yes on Debug I do see the value of SelectedValue.. its from same repository.. not form differnt..thanks
Then update your code, there you load selectedValue from `_obnRepository` and the list data from `_bvRepository`.
Dave
oopss.. so it wont display then?>
What won't display when?
Dave
Ok, maybe I have the solution now :-) Try to rename your DropDown, for example `<%=Html.DropDownList("ServiceTypeListAllDropDown",Model.ServiceTypeListAll)%>`. Does it work then?
Dave
I mean to say if it is different repositroy it wont display? selected value?
Actually it makes no difference where you get the selected value from, it seemed just strange, that you loaded it from a different repository. Did you try the renaming? There seems to be some weird behaviour if the DropDownList is named like one of the Model's properties
Dave
Ohh I ill try to change the Dropdownlist value.. 1 min I will update you..thanks
Yes If i change my dropdown list box name its working now.. why its happend like that?thanks
No idea, why it's like this! :D
Dave
:) but thanks for your all time for supporting me Dave.. I appreciate that.. thanks soo much..