来自asp.net mvc4中单个视图的不同数据库的多个下拉列表

时间:2013-08-18 06:18:17

标签: asp.net

我在网络表单中使用了以下代码,我希望将其转换为带有四个按钮及其事件和网格的asp.net mvc4。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MasterFiles
{
    public partial class WebForm1 : System.Web.UI.Page
    {

//这是连接字符串             public static String connstr =" Data Source = 23.22.136.160; Initial Catalog = Medcare; Persist Security Info = True; User ID = sa; Password = dlink&#34 ;;

//这是绑定下拉列表的常用方法             public static void FillDDList(ref DropDownList DropDownName,String Query)             {

            using (SqlConnection con = new SqlConnection(connstr))
            {
                con.Open();
                SqlDataReader dr;
                try
                {

                    //Check whether the Drop Down has existing items. If YES, empty it.
                    if (DropDownName.Items.Count > 0)
                        DropDownName.Items.Clear();

                    SqlCommand cmd = new SqlCommand(Query, con);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        ListItem extLI = new ListItem();
                        extLI.Value = dr[0].ToString();
                        extLI.Text = dr[1].ToString();
                        DropDownName.Items.Add(extLI);
                    }

                    dr.Close();
                    con.Close();
                }
                catch (Exception ex)
                {
                    con.Close();
                    //RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
                }
            }

        }

//这是绑定网格的方法。             public static bool FillGridBySqlString(ref System.Web.UI.WebControls.GridView GVW,string strSQL)             {                 SqlConnection conn = new SqlConnection();                 System.Data.SqlClient.SqlCommand SqlComm1 = new System.Data.SqlClient.SqlCommand();                 DataSet ds = new DataSet();                 DataTable dt_Kit = new DataTable();                 // DataTable dt_Items = new DataTable();                 SqlDataAdapter ListAdapter = new SqlDataAdapter();

            conn.ConnectionString = connstr;
            SqlCommand dataSelectCommand = new SqlCommand();
            dataSelectCommand.CommandType = CommandType.Text;
            dataSelectCommand.CommandText = strSQL;
            dataSelectCommand.CommandType = System.Data.CommandType.Text;
            dataSelectCommand.Connection = conn;
            ListAdapter.SelectCommand = dataSelectCommand;
            ListAdapter.Fill(dt_Kit);
            dt_Kit.TableName = "Kit";
            ds.Tables.Add(dt_Kit);
            GVW.DataSource = ds;
            GVW.DataMember = "Kit";
            GVW.DataBind();


            try
            {
                dt_Kit.Clear();
                ds.Tables.Remove(dt_Kit);
                ds.Clear();

            }
            catch
            {
            }
            return true;
        }

//这是执行sql命令的方法。             public static void execNonQuerySQL(string sql)             {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connstr;
            DataSet ds = new DataSet();
            SqlCommand dataSelectCommand = new SqlCommand();
            try
            {
                dataSelectCommand.CommandType = CommandType.Text;
                dataSelectCommand.CommandText = sql;
                dataSelectCommand.Connection = conn;
                conn.Open();
                dataSelectCommand.ExecuteNonQuery();
            }
            catch { }
            conn.Close();
            dataSelectCommand.Dispose();
        }
        public static void execNonQuerySSP(string StoredProcName, Object[,] parmarr)
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connstr;
            DataSet ds = new DataSet();
            SqlCommand dataSelectCommand = new SqlCommand();
            try
            {
                dataSelectCommand.CommandType = CommandType.StoredProcedure;
                dataSelectCommand.CommandText = StoredProcName;
                dataSelectCommand.Connection = conn;
                conn.Open();
                for (int i = 0; i < parmarr.Length / 2; )
                {
                    dataSelectCommand.Parameters.AddWithValue("@" + parmarr[i, 0], parmarr[i, 1]);
                    if (parmarr[i++, 0] == null) break;
                }
                dataSelectCommand.ExecuteNonQuery();
            }
            catch { }
            conn.Close();
            dataSelectCommand.Dispose();
        }
        string sqlstr = "select  " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Medcare.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'VMGR' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Base Data in VM/Medcare], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Pils.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'PILS' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in PILS System], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Alg.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'ALG' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in ALG System], " +
               " (Select '(BCO_ID:'+Convert(varchar(5),B.MCM_APP_ID) +') -'+C.BCO_CODE+':'+C.BCO_DESCRIPTION BCO_TEXT From MasterCodesMap B,Boise1.dbo.Billing_Code C Where B.MCM_APP_ACRO = 'USI' AND B.MCM_VM_ID = A.MCM_VM_ID AND C.BCO_ID=B.MCM_APP_ID ) [Mapped Data in USI System] " +
               " from MasterCodesMap A where  MCM_APP_ACRO = 'VMGR'  " +
               " Order by 2 ";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDDList(ref DropDownList1, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from Medcare.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList2, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from PILS.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList3, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from ALG.dbo.Billing_Code Order by 1");
                FillDDList(ref DropDownList4, "SELECT -1,'No Virtual Manager Match' UNION select BCO_ID,BCO_DESCRIPTION + '   =>'+ BCO_CODE BCO_TEXT from Boise1.dbo.Billing_Code Order by 1");

                FillGridBySqlString(ref GridView1, sqlstr);
            }

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Set the hand mouse cursor for the selected row.
                e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

                //////// The seelctButton exists for ensuring the selection functionality
                //////// and bind it with the appropriate event hanlder.
                //////LinkButton selectButton = new LinkButton()
                //////{
                //////    CommandName = "Select",
                //////    Text = e.Row.Cells[0].Text
                //////};
                //////selectButton.Font.Underline = false;
                //////selectButton.ForeColor = System.Drawing.Color.Beige;

                //////e.Row.Cells[3].Controls.Add(selectButton);
                ////////e.Row.Attributes["OnClick"] =
                ////////     Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
                //////e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if(DropDownList1.SelectedIndex != 0)
            {
            Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "VMGR" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList1.SelectedValue}, 
                            };
            execNonQuerySSP("crtMasterCodesMap",parmarr);
            }
            if (DropDownList2.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "PILS" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList2.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            if (DropDownList3.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "ALG" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList3.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            if (DropDownList4.SelectedIndex != 0)
            {
                Object[,] parmarr = {                      
                            { "TABLE_ACRO", "BLCD"}, 
                            { "Orig_MCM_ID", -1 }, 
                            { "App_ACRO", "USI" }, 
                            { "VMID", DropDownList1.SelectedValue}, 
                            { "APP_ID", DropDownList4.SelectedValue}, 
                            };
                execNonQuerySSP("crtMasterCodesMap", parmarr);
            }
            FillGridBySqlString(ref GridView1, sqlstr);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            execNonQuerySQL("delete MasterCodesMap where MCM_VM_ID = " + DropDownList1.SelectedValue);
            FillGridBySqlString(ref GridView1, sqlstr);
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
             Object[,] parmarr = {                      
                              { "iVM_ID", DropDownList1.SelectedValue},
                              { "iTABLE_ACRO", "BLCD"}

                            };
            execNonQuerySSP("synchVMMasterData", parmarr);
            FillGridBySqlString(ref GridView1, sqlstr);
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            FillGridBySqlString(ref GridView1, sqlstr);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过ViewBag

传递您的下拉列表数据
ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");

并进入你的观点:

@Html.DropDownList("ProfileId", String.Empty)

或其他例子:
控制器

public ActionResult Index()
{
            List<SelectListItem> obj1 = new List<SelectListItem>();
            obj1.Add(new SelectListItem { Text = "Text1", Value = "1" });
            obj1.Add(new SelectListItem { Text = "Text2", Value = "2" });
            obj1.Add(new SelectListItem { Text = "Text3", Value = "3" });
            List<SelectListItem> obj2 = new List<SelectListItem>();
            obj2.Add(new SelectListItem { Text = "Text1", Value = "1" });
            obj2.Add(new SelectListItem { Text = "Text2", Value = "2" });
            obj2.Add(new SelectListItem { Text = "Text3", Value = "3" });
            ViewBag.State1 = obj1;
            ViewBag.State2 = obj2;
            return View();
}

查看

@Html.DropDownList(“State1”)
@Html.DropDownList(“State2”)

但在我看来,更好的方法是使用ViewModel解决此问题。