Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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


C#
[AcceptVerbs(HttpVerbs.Post)]
        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;                
                conn.Open();
                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();
                da.Fill(ds);
                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();
                        modelList.Add(model);
                    }             
                conn.Close();             
                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
Updated 13-Aug-12 15:28pm
v2
Comments
Abdul Quader Mamun 13-Aug-12 21:29pm    
edited for code block.

1 solution

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.
 
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