在MVC3中获取已检查的Checkbox和Selected DropDown的值?

时间:2012-07-19 10:19:10

标签: asp.net-mvc-3 checkbox html-select

大家好我必须传递复选框中的选中值和DropDown中的选定值

我的观点看起来像这样

                        Project:DropDown
                   -----------------------------
           EmployeeNames
       --------------------------
      checkbox,AnilKumar
      checkBox,Ghouse

this is my aspx page

       <body>
<% using (Html.BeginForm())
   { %>

    &LT;%:Html.ValidationSummary(真)%&GT;     

<div></div><div style="margin:62px 0 0 207px;"><a style="color:Orange;">Projects : </a><%:Html.DropDownList("Projects")%></div>
<fieldset style="color:Orange;">

    <legend>Employees</legend>
      <% foreach (var item in Model)
         { %>  
              <div>
              <%:Html.CheckBox(item.EmployeeName)%>
              <%:Html.LabelForModel(item.EmployeeName)%>
              </div>
      <%} %>
      <%} %>
</fieldset>
<p>
<input type="button" value="AssignWork" /></p>

   </div>
 </body>

我必须将所有已选中的雇员姓名从选定的项目ID下拉到我的帖子方法中。我可以这样做,任何人都可以帮助我吗

    this is my controller

         [AcceptVerbs(HttpVerbs.Get)]
public ActionResult AssignWork()
{
    ViewBag.Projects = new SelectList(GetProjects(), "ProjectId", "ProjectName");       
    var employee = GetEmployeeList();      
    return View(employee);
}

    public List<ResourceModel> GetEmployeeList()
{
        var EmployeeList = new List<ResourceModel>();
        using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"))
        {
            conn.Open();
            SqlCommand dCmd = new SqlCommand("select EmployeId,EmployeeName from EmployeeDetails", conn);
            SqlDataAdapter da = new SqlDataAdapter(dCmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                var model = new ResourceModel();
                model.EmployeeId = Convert.ToInt16(ds.Tables[0].Rows[i]["EmployeId"]);
                model.EmployeeName = ds.Tables[0].Rows[i]["EmployeeName"].ToString();
                EmployeeList.Add(model);
            }
            return EmployeeList;
        }
}

    public List<ResourceModel> GetProjects()
    {
         var roles = new List<ResourceModel>();
        SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
        SqlCommand Cmd = new SqlCommand("Select ProjectId,projectName from  Projects", 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.ProjectId= Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectId"]);
            model.ProjectName = ds.Tables[0].Rows[i]["projectName"].ToString();               
            roles.Add(model);
        }
          conn.Close();
          return roles ;

    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AssignWork(int projectid,string EmployeeName,FormCollection form,ResourceModel model)
    {

        using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"))
        {
            conn.Open();
            SqlCommand insertcommande = new SqlCommand("AssignWork", conn);
            insertcommande.CommandType = CommandType.StoredProcedure;
            insertcommande.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = EmployeeName;
            insertcommande.Parameters.Add("@projectId", SqlDbType.VarChar).Value = projectid;
            //insertcommande.Parameters.Add("@Status", SqlDbType.VarChar).Value = model.status;
            insertcommande.ExecuteNonQuery();
        }
        return View();

    }

2 个答案:

答案 0 :(得分:0)

我所做的是创建一个jQuery函数,我将其连接到提交按钮的click事件,该事件遍历复选框,抓取它们的id,构建逗号分隔的列表,然后将其发送给控制器。在控制器中,您可以在逗号上拆分字符串,并处理各个项目。我现在无法访问此代码,但如果您需要,可以稍后发布。

您也可以构建一个JSON字符串,而不是逗号分隔列表,以发送回控制器并处理控制器上的JSON字符串。

希望这有帮助。

答案 1 :(得分:0)

我用这个

更改了AssignWork post方法
         [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AssignWork(FormCollection collection, string Projects, string EmployeId)
    {
        ViewBag.Projects = new SelectList(GetProjects(), "ProjectId", "ProjectName");
        if (!string.IsNullOrEmpty(collection["EmployeId"]))
        {
            //formCollection["check"] is a string content checked box value with format value1,value2,valu3....
            string[] temp = collection["EmployeId"].Split(',');
            for (int i = 0; i < temp.Length; i++)
            {
                EmployeId = temp[i];
                if (EmployeId != "false")
                {

                    SqlConnection Assignworkconn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
                    {
                        Assignworkconn.Open();
                        SqlCommand insertcommande = new SqlCommand("AssignWork", Assignworkconn);
                        insertcommande.CommandType = CommandType.StoredProcedure;
                        insertcommande.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = EmployeId;
                        insertcommande.Parameters.Add("@projectId", SqlDbType.Int).Value = Projects;
                        //put your insert code here with temp[i] is checked box value
                        insertcommande.ExecuteNonQuery();
                    }
                }
            }
        }
        return RedirectToAction("AssignWork");
    }