-1

大家好,我必须从复选框中传递选中的值并从下拉菜单中选择值,

我的观点看起来像这样

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

this is my aspx page

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

<%:Html.ValidationSummary(true)%>

<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();

    }
4

2 回答 2

0

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

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

希望这可以帮助。

于 2012-07-19T22:20:37.237 回答
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");
    }
于 2012-07-31T06:56:42.627 回答