大家好我必须从下拉列表中将所选值从控制器中的post方法中获取。我可以这样做吗
这是我的控制器
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddNew()
{
ViewBag.Roles = new SelectList(List(), "RoleID", "RoleName");
return View();
}
//
//Geting All Roles In a GetRoles()/
//
public List<ResourceModel> List()
{
var roles = new List<ResourceModel>();
SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
SqlCommand Cmd = new SqlCommand("Select GroupId,EmplopyeeRole from EmployeeGroup", conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
var model = new ResourceModel();
model.RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]);
model.RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString();
roles.Add(model);
}
conn.Close();
return roles ;
}
//
//To perform the AddNew Logic..i.e it adds a new employee to DB/
//
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNew(ResourceModel model)
{
// var modelList = new List<ProjectModel>();
using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"))
{
conn.Open();
SqlCommand insertcommande = new SqlCommand("InsertEmplyoee", conn);
insertcommande.CommandType = CommandType.StoredProcedure;
insertcommande.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = model.EmployeeName;
insertcommande.Parameters.Add("@EmployeeEmailId", SqlDbType.VarChar).Value = model.EmployeeEmailId;
insertcommande.Parameters.Add("@EmployeePassword", SqlDbType.VarChar).Value = model.EmployeePassword;
insertcommande.Parameters.Add("@GroupName", SqlDbType.VarChar).Value = model.RoleName;
insertcommande.ExecuteNonQuery();
}
return View();
}
现在我想在我的postmethod中获取所选值@GroupName参数...任何人都可以帮助你做怎么做
这是我的HTML
<%:Html.DropDownList("Roles")%>
答案 0 :(得分:0)
您可以让您的POST操作采用视图模型,或者为其添加另一个包含所选值的参数:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNew(ResourceModel model, string roles)
如果您打算重新显示相同的视图,请不要忘记在POST操作中重新填充ViewBag.Roles
,就像在GET操作中一样。
答案 1 :(得分:0)
您可以在控制器操作中使用Request.Form["Roles"]
..但是更好的解决方案是为页面创建新的视图模型,您可以使用Html.DropDownListFor(m => m.RoleID, rolesSelectList)
或其他..并且值将自动生效在udpate方法中绑定到您的模型:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNew(ResourceModel model)