回发时如何设置DropDown的默认值 [英] How to set the Default value for DropDown when post back
本文介绍了回发时如何设置DropDown的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在下拉列表中选择一个值时,我将得到一个回发,并且即使在回发之后,也会选择我选择的值.如何获得默认值<-Select Project->回发后再次作为选定值?在mvc中,我该怎么做请帮助我
像这样的myn控制器
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
[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>
推荐答案
DropDownList方法具有允许您选择所选项目的版本.您还可以在SelectList中的正确项目上将Selected属性设置为true.
The DropDownList method has a version that lets you choose your selected item. You can also set the Selected property to true on the correct item in your SelectList.
这篇关于回发时如何设置DropDown的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文