如何在MVC3中回发时设置DropDown的默认值

时间:2012-08-10 07:41:36

标签: c# asp.net-mvc-3

我正在使用MVC3,我有一个下拉列表,我需要从数据库中设置默认值。

当我在下拉列表中选择一个值时,我会收到一个回发,即使在回发后也会选择我选择的值。如何获取默认值< - 选择项目 - >在回发后再次选择值?这怎么在MVC中完成?

以下是我使用的控制器和视图。

我的模特

namespace BugTracker.Models
{
    public class BugModel
    {
        public List<BugModel> InsertBug {get; set;}
        public List<BugModel> Bugs { get; set; }
        public Int16 BugID { get; set; }
        [Required(ErrorMessage = "Title is required")]
        public string Title { get; set; }
        [Required(ErrorMessage = "Description is required")]
        public string Description {get; set;}
        [Required(ErrorMessage = "Version is required")]
        public string Version { get; set; }
        [Required(ErrorMessage = "BuildNumber is required")]
        public string BuildNumber { get; set; }
        [Required(ErrorMessage = "CreatedDate is required")]
        public string CreatedDate { get; set; }
        [Required(ErrorMessage = "GetDate is required")]
        public string GetDate { get; set; }

        public List<BugModel> GetProjects { get; set; }
        public int ProjectID { get; set; }
        [Required(ErrorMessage = "ProjectName is required")]
        public string ProjectName { get; set; }

        public List<BugModel> GetEmployee {get; set;}
        public int EmployeeID { get; set; }
        public int CreatedByID { get; set; }
        [Required(ErrorMessage = "EmployeeName is required")]
        public string EmployeeName {get;set;}
        [Required(ErrorMessage = "CreatedBy is required")]
        public string CreatedBy { get; set; }

        public List<BugModel> GetCategory { get; set;}
        public int CategoryID { get; set; }
        [Required(ErrorMessage = "Category is required")]
        public string Category { get; set;}

        public List<BugModel> GetSeverity { get; set;}
        public int SeverityID { get; set; }
        [Required(ErrorMessage = "Severity is required")]
        public string Severity { get; set; }

        public List<BugModel> GetPriority { get; set; }
        public int PriorityID { get; set; }
        [Required(ErrorMessage = "Prirority is required")]
        public string Prirority { get; set;}

        public List<BugModel> GetReleasePhase { get; set;}
        public int ReleasePhaseID { get; set; }
        [Required(ErrorMessage = "ReleasePhase is required")]
        public string ReleasePhase { get; set;}

        public List<BugModel> GetTypes { get; set; }
        public int TypeID { get; set; }
        [Required(ErrorMessage = "Type is required")]
        public string Type { get; set; }
 public List<BugModel> GetBugHistory { get; set; }    
        [Required(ErrorMessage = "AssignTo is required")]
        public string AssignTo { get; set; }

    }
}

我的控制器

[AcceptVerbs(HttpVerbs.Post)]    
public ActionResult BugDetails(FormCollection form,string Projects,string Prirority,string CreatedBy,BugModel model)    
{
    var modelList = new List();    
    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();            
            conn.Close();             
           return View(modelList);
        }
    }    

我的观看页面

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--")%>
}

当页面从回发返回时,我希望选择默认值,但在MVC中没有页面加载方法。所以,请有人指出我正确的方向。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

通过使用DropDownListFor(),您可以将所需项目传递到视图中的模型中。

示例:

@{ var projects= (IEnumerable<Projects>)ViewBag.Projects; }


@Html.DropDownListFor(m => m.ProjectId, projects, string.Empty)

简而言之,使用您的模型在下拉列表中确定所选值,通过您的viewbag提供可能的值。 如果模型上的属性是属性类型的默认值,则dropdownlistfor将显示带有可能值的列表的第一个值。