当前位置: 首页 > 知识库问答 >
问题:

asp中单个视图上不同数据库的多个下拉列表。净mvc4

卜勇
2023-03-14

下面是我在web表单中使用的代码,我想将其转换为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”;

//这是绑定下拉列表公共静态void FillDDList(ref DropDownList dropdownlame,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=新的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命令的方法。

            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个答案

阳枫涟
2023-03-14

您可以通过ViewBag将您的数据传递给dropDownlist:

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解决这个问题。

 类似资料:
  • 问题内容: 我需要使用JavaScript根据下拉菜单A中的选择更改下拉菜单B的内容。没有涉及到数据库查询- 我事先知道应该在A中选择B的内容。我已经找到了一些使用AJAX的示例,但是由于没有涉及到数据库查询,所以没有必要。谁能为我指出一些示例代码以实现此目的? 问题答案: function configureDropDownLists(ddl1, ddl2) {

  • 问题内容: 我有这个数据库存储传感器采集数据, 采集(acq)来自不同控制单元(cu)的固定间隔(日期时间) 每个采集在数据表中存储了许多不同的度量 我需要这种观点: 作为: v1是data.value(例如湿度),其中 acq.id_cu = 1 AND data.id_meas = 100 v2是data.value(例如计数器),其中 acq.id_cu = 2 AND data.id_me

  • 在一个电子表格中,我有50张表格,代表50个州。在这些州中,我正试图从一个输入列(所有表格上的Col4,列'City'格式如下:'Austin,TX')合并到一个索引列中,以构建一个城市地图。(我在谷歌表单中使用了标记地图)。 然而,为了完成这项任务,我不断地遇到错误。 我已经使用拉入了两张纸(两张纸使其更容易)。 然而,我得到一个 “查询已完成,输出为空”错误。 工作,但我只得到了AL city

  • 问题内容: 我需要比较2个不同数据库中的数据库表,以了解差异所在,是否有一个简单的工具或脚本来实现? 问题答案: redgate SQL数据比较

  • 我正在对php页面进行调整,希望最终用户能够从下拉列表中选择“姓名和电子邮件”。数据将来自mySQL数据库。 我已经设法让它部分工作,但是必须有东西做一个"验证",因为当页面上的所有数据都被选中,最终用户试图提交页面时,最终用户会收到电子邮件地址是无效,而事实上它是100%有效的。 与此相关,我更改的部分是,最终用户过去必须手动键入他们的电子邮件地址,现在他们可以从下拉列表中选择,但现在它告诉我们

  • 问题内容: 我正在建立一个网站,其中包含不同类型的项目,例如博客,帖子,文章等。用户可以将其中任何一个设置为他/她的最爱。现在,当我处理这个问题时,我有两个选择 为每种对象的用户收藏夹创建一个表。 为所有用户的所有类型的对象创建一个公用表。 第一种结构的问题是,我将不得不查询很多表以显示特定用户的收藏夹。但这将使我可以轻松地将收藏夹分为不同的类别。 但是,如果我必须在一个页面上显示所有收藏夹并将它