Click here to Skip to main content
15,885,767 members

How to set the Default value for DropDown when post back

shakil4u asked:

Open original thread
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>
Tags: MVC3

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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