0

I have a dropdownlist which is binding with TempData.when it is coming first time it is showing all values.After selecting a particular value that value is saving in the database correctly.But the selected value is not showing.I am giving my code below.

For retrieving I have written in index action controller

TempData["Clients"] = (IEnumerable<SelectListItem>)ClientService.GetALLClientsName().Select(C=>new SelectListItem { Value=C.CLIENT_ID.ToString(),Text=C.CLIENT_NAME});

when retrieving after editing I can not get the selected value.I have written in razor like this below

@if (TempData["SelectedClientName"] != null && TempData["SelectedClientId"] != null)
                       {
                           foreach (SelectListItem sli in lstClients)
                           {
                               if (sli.Value.Equals(TempData["SelectedClientId"].ToString()))
                               {
                                   sli.Text = TempData["SelectedClientName"].ToString();
                                   sli.Value = TempData["SelectedClientId"].ToString();
                                   sli.Selected = true;
                                   break;    

                               }
                           }

                       }

                      @Html.DropDownList("drpClientName", lstClients, "--Select--")

I have converted TempData["Clients"] in lstClients.Please help me.

amitabha
  • 646
  • 1
  • 9
  • 20

3 Answers3

1
public ActionResult Index(){
    var selectedClientId = 5; // for example (change it with your variable)
    ViewBag.Clients = new SelectList(ClientService.GetALLClientsName(), 
                              "CLIENT_ID", "CLIENT_NAME", selectedClientId)
}

View:

@Html.DropDownList("drpClientName", (SelectList)ViewBag.Clients, "--Select--")

TempData has different usage. It's behavior is like Session but it survives only next request. It's useful when redirecting and want to pass data. See this link to better understand the difference.

Community
  • 1
  • 1
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • @amitabha I don't know his code in controller, that's example how to do it right. He will change my hardcoded value to his own variable value. I have written comment "for example" to note this :) – karaxuna Jun 19 '13 at 07:01
1

You should use DropDownListFor and ViewModel contains ClientId field:

YourViewModel:

public class YourViewModel{
    public int ClientId {get;set;}
}

View:

@Html.DropDownListFor( x => x.ClientId, new SelectList( Clients.GetClientsList(Model.ClientId), "Value", "Text", Model.ClientId))

in Clients.cs (such as):

public static List<SelectListItem> GetClientsList(int client)
{
    var dataContext = new YourDataContext(  );
    var data = dataContext.GetModelsFn(client).ToList();

    var result = ( from res in data
               select new SelectListItem()
                          {
                              Text = res.ClientName,
                              Value = res.ClientId.ToString(),
                              Selected = res.ClientId == client
                          } ).ToList();

    return result;
}
Xordal
  • 1,369
  • 13
  • 29
0

you Can set that at model in controller.ie When you are creating TempData["Clients"]

set the selectlistitem that you want as selected.

Anish Karunakaran
  • 987
  • 3
  • 17
  • 39