0

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

当我在下拉列表中选择一个值时,我得到一个回发,即使在回发之后,我选择的值也被选中。如何在回发后再次获取默认值 <--Select Project--> 作为选定值?这在 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 中没有页面加载方法。所以,请有人指出我正确的方向。

先感谢您。

4

1 回答 1

0

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

例子:

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


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

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

于 2012-08-10T09:31:32.117 回答