[MVC4] MainMenu1: get DropDownList from database by returning whole view

  • 1191
  • 0

製作一個主選單, 在onload時從資料庫載入Customer/Application/Line/Station, 並把DropDownList的預設值設為上次登入的結果, 選擇不同的Customer/Application會帶出不同的Line/Station, 部分站別要求輸入Username/Password, Submit後會儲存登入資訊至Cookie/Session, 儲存至Cookie是為了下次的訪問, 儲存至Session是為了其他頁面的取用, 此外若無身分驗證失敗的訊息, 會依Applicatio抵達不同的Controller

製作一個主選單, 在onload時從資料庫載入Customer/Application/Line/Station, 並把DropDownList的預設值設為上次登入的結果, 選擇不同的Customer/Application會帶出不同的Line/Station, 部分站別要求輸入Username/Password, Submit後會儲存登入資訊至Cookie/Session, 儲存至Cookie是為了下次的訪問, 儲存至Session是為了其他頁面的取用, 此外若無身分驗證失敗的訊息, 會依Applicatio抵達不同的Controller

 

STP1. ~/Models/Home.cs, 建立Model存取DB, Connection字串由Web.Config取得, 引用QMSSDK連結DB


  <appSettings>
    <add key="strJONConn" value="Data Source=XXX.XXX.XXX.XXX;Initial Catalog=JON;User ID=XXXXX;Password=XXXXX;Persist Security Info=True;Network Library=DBMSSOCN"/>
    <add key="strSTNConn" value="Data Source=XXX.XXX.XXX.XXX;Initial Catalog=STN;User ID=XXXXX;Password=XXXXX;Persist Security Info=True;Network Library=DBMSSOCN"/>
    <add key="strNEOConn" value="Data Source=XXX.XXX.XXX.XXX;Initial Catalog=NEO;User ID=XXXXX;Password=XXXXX;Persist Security Info=True;Network Library=DBMSSOCN"/>
    <add key="strAIOConn" value="Data Source=XXX.XXX.XXX.XXX;Initial Catalog=AIO;User ID=XXXXX;Password=XXXXX;Persist Security Info=True;Network Library=DBMSSOCN"/>
    <add key="jQueryTheme" value=""/>
  </appSettings>

public class Home : QMSSDK.Db.Specific
    {
        private SqlConnection cn;
        private string strSQL;

        public string Customer { get; set; }
        public string Application { get; set; }
        public string Line { get; set; }
        public string Station { get; set; }

        //Constructor
        public Home()
        {
            String conn = ConfigurationManager.AppSettings["strJONConn"].ToString();
            this.cn = QMSSDK.Db.Connections.CreateNormalCn(conn);
        }

        public DataTable getCustomer()
        {
            strSQL = "EXEC [MVC_getMainMenu] @Type='Customer'";
            DataTable dt = this.Execute(strSQL, this.cn).Tables[0];
            return dt;
        }

        public DataTable getApplication()
        {
            strSQL = "EXEC [MVC_getMainMenu] @Type='Application'";
            DataTable dt = this.Execute(strSQL, this.cn).Tables[0];
            return dt;
        }

        public DataTable getLine(String customer)
        {
            strSQL = "EXEC [MVC_getMainMenu] @Type='Line', @Customer='" + customer + "'";
            DataTable dt = this.Execute(strSQL, this.cn).Tables[0];
            return dt;
        }

        public DataTable getStation(String customer, String application)
        {
            strSQL = "EXEC [MVC_getMainMenu] @Type='Station', @Customer='" + customer + "', @Application='" + application + "'";
            DataTable dt = this.Execute(strSQL, this.cn).Tables[0];
            return dt;
        }

     }

 

STP2. ~/Controllers/HomeController.cs, 在ActionResult裡, 取得Cookie的值作為DropDownList的預設值, 動態設定DropDownList必須透過List<SelectListItem>及SelectListItem, 透過SelectListItem.Selected = true可設定DropDownList的預設值, 最後, 透過ViewData傳遞資料給View 


public class HomeController : Controller
    {
        static string defaultCustomer = "";
        static string defaultApplication = "";
        static string defaultLine = "";
        static string defaultStation = "";
        static string defaultUser = "";

        Home home = new Home();

        public ActionResult Index()
        {
            HttpCookie httpCookie = Request.Cookies["QMS"];
            if (httpCookie != null)
            {
                defaultCustomer = httpCookie["Customer"];
                defaultApplication = httpCookie["Application"];
                defaultLine = httpCookie["Line"];
                defaultStation = httpCookie["Station"];
                defaultUser = httpCookie["User"];
            }

            ViewData["slCustomer"] = getDefaultCustomer(defaultCustomer);
            ViewData["slApplication"] = getDefaultApplication(defaultApplication);
            ViewData["slLine"] = getDefaultLine(defaultCustomer, defaultLine);
            ViewData["slStation"] = getDefaultStation(defaultCustomer, defaultApplication, defaultStation);
            ViewData["tbUser"] = defaultUser;
            ViewData["lbMsg"] = "";

            return View();
        }

        #region Functions
        public List getDefaultCustomer(string defaultCustomer)
        {
            DataTable dsCustomer = home.getCustomer();
            List slCustomer = new List();
            foreach (DataRow dr in dsCustomer.Rows)
            {
                SelectListItem item = new SelectListItem();
                item.Text = dr["Customer"].ToString();
                item.Value = dr["Customer"].ToString();
                item.Selected = (item.Text.Equals(defaultCustomer)) ? true : false;      
                slCustomer.Add(item);
            }
            return slCustomer;
        }

        public List getDefaultApplication(string defaultApplication)
        {
            DataTable dsApplication = home.getApplication();
            List slApplication = new List();
            foreach (DataRow dr in dsApplication.Rows)
            {
                SelectListItem item = new SelectListItem();
                item.Text = dr["Application"].ToString();
                item.Value = dr["Application"].ToString();
                item.Selected = (item.Text.Equals(defaultApplication)) ? true : false;
                slApplication.Add(item);
            }
            return slApplication;
        }

        public List getDefaultLine(string selectedCustomer, string defaultLine) 
        {
            DataTable dsLine = home.getLine(selectedCustomer);
            List slLine = new List();
            foreach (DataRow dr in dsLine.Rows)
            {
                SelectListItem item = new SelectListItem();
                item.Text = dr["Line"].ToString();
                item.Value = dr["Line"].ToString();
                item.Selected = (item.Text.Equals(defaultLine)) ? true : false;
                slLine.Add(item);
            }
            return slLine;
        }

        public List getDefaultStation(string selectedCustomer, string selectedApplication, string defaultStation)
        {
            DataTable dsStation = home.getStation(selectedCustomer, selectedApplication);
            List slStation = new List();
            foreach (DataRow dr in dsStation.Rows)
            {
                SelectListItem item = new SelectListItem();
                item.Text = dr["Station"].ToString();
                item.Value = dr["Station"].ToString();
                item.Selected = (item.Text.Equals(defaultStation)) ? true : false;
                slStation.Add(item);
            }
            return slStation;
        }
        #endregion

    }

 

STP3. ~/Views/Home/Index.cshtml, 取得ViewData的值並設定給DropDownList 


@model MVC_SFS_1.Models.Home
@{ Layout = null;}

<!DOCTYPE html>
@{
    var slCustomer = (List<SelectListItem>)ViewData["slCustomer"];
    var slApplication = (List<SelectListItem>)ViewData["slApplication"];
    var slLine = (List<SelectListItem>)ViewData["slLine"];
    var slStation = (List<SelectListItem>)ViewData["slStation"];
}

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="/Scripts/jquery-1.8.2.js"></script>
    <link href=@Url.Content("~/Content/Home.css") rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <div class="header">Welcome to QMS MVC Shop Floor</div>
        <div class="content">
            @using (Html.BeginForm("Index", "Home", FormMethod.Post))
            {               
                <table style="width: 300px">
                    <tr>
                        <td>@Html.Label("Customer")</td>
                        <td>@Html.DropDownList("dlCustomer", slCustomer)</td>
                    </tr>
                    <tr>
                        <td>@Html.Label("Application")</td>
                        <td>@Html.DropDownList("dlApplication", slApplication)</td>
                    </tr>
                    <tr>
                        <td>@Html.Label("Line")</td>
                        <td>@Html.DropDownList("dlLine", slLine)</td>
                    </tr>
                    <tr>
                        <td>@Html.Label("Station")</td>
                        <td>@Html.DropDownList("dlStation", slStation)</td>
                    </tr>
                     <tr>
                        <td></td>
                        <td>
                            <div class="bottom"><input type="submit" value="Submit" />
                            <input type="button" value="Close"  onclick="{ window.close(); }" /></div>
                        </td>
                    </tr>
                </table>              
            }
        </div>       
    </div>
</body>
</html>