Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two controllers and i am passing data one to another. When i pass data second controller to first controller dropdown's selected value disappears. I googled many hours but i failed.How can i keep selected ListItem when user clicks button and after page refreshes? Here is my codes:

First controller which has selectlist and controlling codes.

First Controller:

List<SelectListItem> list= new List<SelectListItem>
            {
            new SelectListItem {  Text = "--- Seçiniz----", Value = ""},
            new SelectListItem {  Text = "text1", Value = "11"},
            new SelectListItem { Text = "text2", Value = "12"},
            new SelectListItem {Text = "text3", Value = "13"},
            new SelectListItem {Text = "text4", Value = "14"},
            new SelectListItem { Text = "text5", Value = "15"}
            };

            if (TempData["daire"] != null)
            {

            int daire = Convert.ToInt32(TempData["daire"]);
            ViewBag.daire = new SelectList(list, "Value", "Text", TempData["daire"]);
            model= (folderBL.getAll().Where(k=>k.Id==daire)).ToList();
            }
           else
            {
            ViewBag.daire = new SelectList(list, "Value", "Text","");
            model= folderBL.getAll();
            }


Second controller, I get the daire value for my SelectList and send data to first controller.

SQL
public ActionResult SecondController(string daire)
       {
           TempData["daire"] = daire;
           return RedirectToAction("FirstController");
       }


And View.

@using (Html.BeginForm("SecondController","MyDashboard",FormMethod.Post))
          {
           @Html.DropDownList("daire", (SelectList)ViewBag.daire, new { id = "mydrop" })

           <td><input type="submit" value="Send" />
          }
Posted

You can use :

@Html.DropDownList("daire", (SelectList)ViewBag.daire, "--- Seçiniz----")

or

@Html.DropDownListFor(m => m.daire, new SelectList(ViewBag.daire, "Value", "Text"), "--- Seçiniz----", new { @id = "mydrop" })
 
Share this answer
 
Comments
Fatihx 7-Aug-14 5:13am    
thank you for your answer.

when i click the button it returns

"---seçiniz---".

failed again.

i am sorry, i wrote "controller" instead "action". please think action instead controllers.
Here is the solution if someone needs it. Just change the View

@using (Html.BeginForm("SecondAction", "MyDashboard", FormMethod.Post))
{
@Html.DropDownList("daire")
<input type="submit" value="Send" />
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900