我正在使用MVC3,並且我有一個下拉列表,我需要從數據庫中設置默認值。如何在MVC3中回發時爲DropDown設置默認值
當我在下拉列表中選擇一個值時,我得到一個回傳,並且我選擇的值甚至在回發之後被選中。我如何獲得默認值,< - 在回發後再次選擇項目 - >作爲選定值?這是如何在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--")%>
}
當第e頁面從回發返回,我想要選擇默認值,但在MVC中沒有頁面加載方法。所以,請有人指出我正確的方向。
預先感謝您。
謝謝Miika l – 2012-08-10 08:14:40