如何检查mvc控制器中是否选中了复选框

时间:2017-12-12 14:27:50

标签: asp.net-mvc

我从表格数据库和复选框中提取项目列表以检查和批准每个项目;但即使我检查项目,它也会抛出此错误消息:请选择至少一个请求的项目。我想要实现的是用户检查列表中的任意数量的项目,然后状态请求编号更新为0.

 public ActionResult RequisitionList(List<Requisition> postingObj)

    {

        IssueDAO dbObj = new DAO(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        List<string> reqNumbers = new List<string>();
        bool check=false;


        foreach (var item in postingObj)
        {
            if (item.postTrnx)
            {
                reqNumbers.Add(item.reqNumber);
            }

        }

        if (check == true)
        {
            dbObj.SetRequisitionStatus0(reqNumbers);
            ViewBag.Message = "Approval Successful!";

        }
        else {
            ViewBag.Message = "Please select at least one requested item";
            return View(dbObj.GetAllRequest());
        }
        return View(dbObj.GetAllRequest());
    }

 public void SetRequisitionStatus0(List<string> reqNumbers)
    {
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand();
        command.CommandText = "requisition_sp_setstatus0";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@reqNumber", SqlDbType.VarChar);
        command.Parameters.Add("@approve_date", SqlDbType.DateTime).Value = DateTime.Now;
        using (command.Connection = connection)
        {
            try
            {
                connection.Open();
                foreach (var item in reqNumbers)
                {
                    command.Parameters["@reqNumber"].Value = item;
                    command.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
        return;

    }


    public List<Requisition> GetAllRequest()
    {
        using (var connection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand("getallrequests", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                List<Requisition> request = new List<Requisition>();

                SqlDataReader rdrObj;

                connection.Open();
                rdrObj = command.ExecuteReader();
                while (rdrObj.Read())
                {
                    Requisition requisition = new Requisition();


                    requisition.reqNumber = rdrObj.GetString(0);
                    requisition.reqDate = rdrObj.GetDateTime(1);

                   requisition.items = getRequestItemByRquisition(rdrObj.GetString(0));
                    request.Add(requisition);
                }
                rdrObj.Close();


                return request;
            }
        }
    }

0 个答案:

没有答案