我正在使用MVC3。 我有一个下拉列表,我需要从数据库中设置默认值。
当我在下拉列表中选择一个值时,我会收到一个回发,即使在回发后也会选择我选择的值。如何在回发后再次将默认值<--Select Project-->
作为选定值?
模型
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中我们没有页面加载方法,所以我该怎么做呢?