Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: MVC3
When I select a value in the dropdownlist I get a postback and the value I selected is selected even after the postback. How do I get the default value, <--Select Project--> as selected value again after the postback? in mvc how do i do this plz help me
myn controlers like this

        public ActionResult BugDetails(FormCollection form,string Projects,string  Prirority,string CreatedBy,BugModel model)
            var modelList = new List<bugmodel>();
            ViewBag.Projects = new SelectList(GetProjects(), "ProjectId", "ProjectName");
            ViewBag.Prirority = new SelectList(GetPriority(), "PriorityID", "Prirority");
            ViewBag.CreatedBy = new SelectList(GetEmployee(), "EmployeeID", "EmployeeName");
            using (SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugtrackerNew;Data Source=SSDEV5-HP\SQLEXPRESS"))
                SqlCommand dCmd = new SqlCommand("Filter", conn);
                dCmd.CommandType = CommandType.StoredProcedure;                
                dCmd.Parameters.Add(new SqlParameter("@ProjectID", Projects));
                dCmd.Parameters.Add(new SqlParameter("@PriorityID",Prirority));
                dCmd.Parameters.Add(new SqlParameter("@CreatedByID",CreatedBy));
                SqlDataAdapter da = new SqlDataAdapter(dCmd);
                DataSet ds = new DataSet();
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                        model.BugID = Convert.ToInt16(ds.Tables[0].Rows[i]["BugID"]);
                        model.Title = ds.Tables[0].Rows[i]["Title"].ToString();
                        model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
                        model.ProjectName = ds.Tables[0].Rows[i]["ProjectName"].ToString();
                        model.Version = ds.Tables[0].Rows[i]["Version"].ToString();
                        model.BuildNumber = ds.Tables[0].Rows[i]["BuildNumber"].ToString();
                        model.Category = ds.Tables[0].Rows[i]["Category"].ToString();
                        model.CreatedDate = ds.Tables[0].Rows[i]["CreatedDate"].ToString();
                        model.Severity = ds.Tables[0].Rows[i]["Severity"].ToString();
                        model.Prirority = ds.Tables[0].Rows[i]["Priorities"].ToString();
                        model.ReleasePhase = ds.Tables[0].Rows[i]["ReleasePhase"].ToString();
                        model.Type = ds.Tables[0].Rows[i]["Types"].ToString();
                        model.CreatedBy = ds.Tables[0].Rows[i]["CreatedByID"].ToString();
                        model.AssignedTo = ds.Tables[0].Rows[i]["AssignedTo"].ToString();
                        model.Status = ds.Tables[0].Rows[i]["ToStatus"].ToString();
                return View(modelList);

myn view page is 

 using (Html.BeginForm())
                         { %>  
                            <%: Html.DropDownList("Projects", (SelectList)ViewBag.Projects)%>                           
                               <%: Html.DropDownList("Prirority", (SelectList)ViewBag.Prirority, "Select Project")%>  
                               <%: Html.DropDownList("CreatedBy", (SelectList)ViewBag.CreatedBy, "--Select Project--")%></bugmodel>
Posted 9-Aug-12 22:18pm
Edited 13-Aug-12 16:28pm
Abdul Quader Mamun at 13-Aug-12 21:29pm
edited for code block.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The DropDownList method has a version that lets you choose your selected item. You can also set the Selected property to true on the correct item in your SelectList.

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

  Print Answers RSS
0 DamithSL 300
1 Zoltán Zörgő 170
2 OriginalGriff 150
3 Sergey Alexandrovich Kryukov 120
4 Maciej Los 115
0 OriginalGriff 7,740
1 DamithSL 5,769
2 Sergey Alexandrovich Kryukov 5,424
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539

Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 13 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100