Click here to Skip to main content
13,048,805 members (80,537 online)
Rate this:
Please Sign up or sign in to 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

        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 21:18pm
Updated 13-Aug-12 15:28pm
Abdul Quader Mamun 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Aug 2012
Copyright © CodeProject, 1999-2017
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