如何在插入到MVC3中的数据库控制器中获取CheckBox选择的值?

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

标签: asp.net-mvc-3 c#-4.0

大家好我必须将复选框中的选定值从我的post方法中导入到数据库控制器中。我怎么能这样做?

My Model Code
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Collections;

namespace Gridview_BugTracker.Models
{
    public class BugTracker_DataHelper
    {

        public static List<BugTracker_DataHelper> GetList{get;set;}

           public int ProjectId { get; set; }
           public string projectName { get; set; }           
           public string Description { get; set; }
           public  string status { get; set; }
           public int EmployeId { get; set; }
           public string EmployeName { get; set; }
           public bool selectChkbox { get; set; }         
      }      

   }

    My Control code
    ------------------------------

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Data;
    using Gridview_BugTracker.Models;
    using System.Data.SqlClient;
    using System.Data.Entity;

    namespace Gridview_BugTracker.Controllers
    {
        public class ProjectsController : Controller
        {
            //
            // GET: /Projects/
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public ActionResult Index()
            {
                var bugedlist = GetList();
                return View(bugedlist);
            }

            /// <summary>
            /// The Bind data in Gridview
            /// </summary>
            /// <returns></returns>
            public List<BugTracker_DataHelper> GetList()
            {
                var modelList = new List<BugTracker_DataHelper>();
                using (SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
                {
                    conn.Open();
                    SqlCommand dCmd = new SqlCommand("Select * from Projects", 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 BugTracker_DataHelper();
                        model.ProjectId = Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectId"]);
                        model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
                        model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
                        model.status = ds.Tables[0].Rows[i]["Status"].ToString();
                        modelList.Add(model);
                    }
                }
                return modelList;
            }

            /// <summary>
            /// Assign The Project Display
            /// </summary>
            /// <returns></returns>
            [AcceptVerbs(HttpVerbs.Get)]
            public ViewResult AssignProject()
            {

                ViewBag.ProjectId = new SelectList(Assinedwork(), "ProjectId", "projectName");           
                var employees = CheckBoxBindEmpName();
                return View(employees);
            }

            /// <summary>
            /// Assignwork
            /// </summary>
            /// <returns></returns>        
            public List<BugTracker_DataHelper> Assinedwork()
            {
                var modelList = new List<BugTracker_DataHelper>();

                using (SqlConnection assignedconn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
                {
                    {
                        assignedconn.Open();
                        SqlCommand Passnedcmd = new SqlCommand("select ProjectId,projectName from Projects", assignedconn);
                        SqlDataAdapter da = new SqlDataAdapter(Passnedcmd);
                        DataSet ds = new DataSet();
                        da.Fill(ds);

                        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                        {
                            var model = new BugTracker_DataHelper();
                            model.ProjectId = Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectId"]);
                            model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
                            modelList.Add(model);
                        }
                    }
                    return modelList;
                }
            }

            public List<BugTracker_DataHelper> CheckBoxBindEmpName()
            {
                var modecheckbox = new List<BugTracker_DataHelper>();

                using (SqlConnection assignedconn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
                {
                    {
                        assignedconn.Open();
                        SqlCommand Chkcmd = new SqlCommand("select EmployeId,EmployeeName from EmployeeDetails", assignedconn);
                        SqlDataAdapter Chkda = new SqlDataAdapter(Chkcmd);
                        DataSet chkds = new DataSet();
                        Chkda.Fill(chkds);

                        for (int i = 0; i <= chkds.Tables[0].Rows.Count - 1; i++)
                        {
                            var modelchk = new BugTracker_DataHelper();
                            modelchk.EmployeId = Convert.ToInt16(chkds.Tables[0].Rows[i]["EmployeId"]);
                            modelchk.EmployeName = chkds.Tables[0].Rows[i]["EmployeeName"].ToString();
                            modecheckbox.Add(modelchk);
                        }

                    }
                }
                return modecheckbox;
         }
        }

    }
    My Assinedcode View code
    ------------------------

     <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>>" %>

    <!DOCTYPE html>

    <html>
    <head runat="server">
        <title>AssignProject</title>
    </head>
    <body>
        <div>
         <% using (Html.BeginForm()) 
         { %>      
        <%:Html.ValidationSummary(true)%>
         <fieldset>
            <legend>Projects</legend>
                        <%: Html.DropDownList("ProjectId")%>
                        <%: Html.ValidationMessage("ProjectId")%>                   
                        <fieldset style="color:Blue;">                 

                        <% foreach (var item in Model) { %>  
                      <div>
                      <%:Html.CheckBox(item.EmployeName) %>
                      <%:Html.LabelForModel(item.EmployeName) %>
                      </div>                 
                    <%} %>
                          <p>
                          <input type="submit" value="Assign" />
                         </p>      

            </fieldset>
        <%} %>

        </div>
    </body>
    </html>

现在我想在我的postmedthod中插入数据库中获取所选值EmployeeName Parameter。任何人都可以帮我解决这个问题吗?

0 个答案:

没有答案