0

こんにちはすべて私はドロップダウンからコントローラーの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")%>
4

2 に答える 2

0

POSTアクションにビューモデルを取得させるか、選択した値を保持する別のパラメーターを追加することができます。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNew(ResourceModel model, string roles)

ViewBag.Rolesまた、同じビューを再表示する場合は、GETアクションで行ったのと同じ方法でPOSTアクションにを再入力することを忘れないでください。

于 2012-07-19T06:31:39.410 に答える
0

コントローラアクションで使用できRequest.Form["Roles"]ますが、おそらくより良い解決策は、ページの新しいビューモデルを作成することであり、使用することもできますHtml.DropDownListFor(m => m.RoleID, rolesSelectList)。値は、udpateメソッドでモデルに自動的にバインドされます。

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult AddNew(ResourceModel model) 
于 2012-07-19T06:34:15.063 に答える