[MVC4-基礎] 連動DropDownList - 使用jQuery、JSON
剛開始學MVC4,以下是一些基礎的學習筆記!
先展示一下結果:
1.選擇申請部門

2.選好後申請部門鎖住防止USER修改並載入該部門所擁有的設備類型

一、資料庫
dept

mf_fx

二、View (Index.cshtml)
@model IEnumerable<LES.Models.Repair>
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
$(document).ready(function () {
$(".DropDownList").append("<option value='Value'>請選擇</option>"); //預設都加入「請選擇」
$(".DropDownList").change(function () {
//讓下拉選單選中即關閉
$(this).attr("disabled", true);
//取出元素ID
var id = $(this).attr('id');
//根據選中的元素執行相對應的動作
switch (id) {
case "dept": {
//選擇部門後把設備類型資料載入
$(function () {
$.getJSON("/Repair/GetDeviceType?dept=" + dept.value, null, function (data) {//向Controller取得GetDeviceType資料
$.each(data, function (i, itemvalue) {//循環加入item
$("#deviceType").append(
$("<option></option>").val(itemvalue.Value).html(itemvalue.Text))
});
});
});
break;
}
case "deviceType": {
//選擇設備類型後把設備編號資料載入
$(function () {
$.getJSON("/Repair/GetDeviceId?dept=" + dept.value + "&type=" + deviceType.value, null, function (data) {//向Controller取得GetDeviceId資料
$.each(data, function (i, itemvalue) {//循環加入item
$("#deviceId").append(
$("<option></option>").val(itemvalue.Value).html(itemvalue.Text))
});
});
});
break;
}
}
});
});
$(function () {
$.getJSON("/Repair/GetDept", null, function (data) {//向Controller取得GetDept資料
$.each(data, function (i, itemvalue) {//循環加入item
$("#dept").append(
$("<option></option>").val(itemvalue.Value).html(itemvalue.Text))
});
});
});
</script>
<h2>維修申請單</h2>
<p>
@Html.Label("dept","申請部門")
<select class="DropDownList" id="dept" name="dept"></select>
<br />
@Html.Label("deviceType","設備類型")
<select class="DropDownList" id="deviceType" name="deviceType"></select>
<br />
</p>
三、Controller (RepairController.cs)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LES.Models;
using System.Data.SqlClient;
using System.Configuration; namespace LES.Controllers
{
public class RepairController : Controller
{
string ErpString = ConfigurationManager.ConnectionStrings["ERPConnection"].ConnectionString; //
// GET: /Repair/ public ActionResult Index()
{ SqlConnection DbErp = new SqlConnection(ErpString); //創建資料庫連線
SqlCommand cmd = new SqlCommand("select dep,name from dept",DbErp); //輸入SQL命令
DbErp.Open(); //開啟資料庫連線 var reader = cmd.ExecuteReader(); //取出結果集 List<SelectListItem> depts = new List<SelectListItem>(); while(reader.Read()){ //逐筆讀出資料寫入List
//(0)=dep ; (1)=name
depts.Add(new SelectListItem { Text = reader.GetString(), Value = reader.GetString()});
} DbErp.Close(); //關閉資料庫連線 ViewBag.DeptType = depts; return View();
} // GET: /Repair/GetDept
public JsonResult GetDept()
{
SqlConnection DbErp = new SqlConnection(ErpString); //創建資料庫連線
SqlCommand cmd = new SqlCommand("select dep,name from dept", DbErp); //輸入SQL命令
DbErp.Open(); //開啟資料庫連線 var reader = cmd.ExecuteReader(); //取出結果集 List<SelectListItem> depts = new List<SelectListItem>(); while (reader.Read())
{ //逐筆讀出資料寫入List
//(0)=dep ; (1)=name
depts.Add(new SelectListItem { Text = reader.GetString(), Value = reader.GetString() });
} DbErp.Close(); //關閉資料庫連線 return this.Json(depts, JsonRequestBehavior.AllowGet);
} // GET: /Repair/GetDeviceType?dept=部門代號
public JsonResult GetDeviceType(string dept)
{
SqlConnection DbErp = new SqlConnection(ErpString); //創建資料庫連線
DbErp.Open(); //開啟資料庫連線
SqlCommand cmd = DbErp.CreateCommand(); //創建命令物件
cmd.CommandText = "select distinct name from mf_fx where dep = @dept"; //SQL語句
cmd.Parameters.AddWithValue("dept", dept); //加入@dept參數 var reader = cmd.ExecuteReader(); //取出結果集 List<SelectListItem> types = new List<SelectListItem>(); while (reader.Read())
{ //逐筆讀出資料寫入List
//(0)=name
types.Add(new SelectListItem { Text = reader.GetString(), Value = reader.GetString() });
} DbErp.Close(); //關閉資料庫連線 return this.Json(types, JsonRequestBehavior.AllowGet);
}
}
}
後來更新一下代碼,新的初始狀態如下:

[MVC4-基礎] 連動DropDownList - 使用jQuery、JSON的更多相关文章
- jQuery基礎知識
jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...
- BootStrap基礎知識
BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...
- CSS1-3基礎知識
CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...
- Python 基礎 - if else流程判斷
hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...
- GO語言基礎教程:序章
首先自我介紹一下我自己,我是一個coder,目前主要從事B/S程序開發工作,懂點PHP;ASP;JSP;JS;VB;C;DELPHI;JAVA,另外知道幾個數據庫,除此之外別無所長,那麼我為何會選擇學 ...
- JavaScript基礎知識
JavaScript基礎知識 1.標籤組使用 <script charset='utf-8' //設置字元集 defet //使腳本延遲到文檔解析完成,Browser已忽略 language=' ...
- 邁向IT專家成功之路的三十則鐵律 鐵律八:IT人學習之道-基礎功
修練過中國武術的人都知道,任何一種拳法的學習最重要的就是基礎功,而基礎功又可分為內在與外在的修練,內在的修練強調在平心.靜氣.不爭的調息.至於外在這首重在站樁.鬆沉.不疾不徐的應對能力.有了深厚基礎的 ...
- 基于Bootstrap的DropDownList的JQuery组件的完善版
在前文 创建基于Bootstrap的下拉菜单的DropDownList的JQuery插件 中,实现了DropDownList的JQuery组件,但是留有遗憾.就是当下拉菜单出现滚动条的时候,滚动条会覆 ...
- 创建基于Bootstrap的下拉菜单的DropDownList的JQuery插件
Bootstrap是当下流行的前端UI组件库之一.利用Bootstrap,可以很方便的构造美观.统一的页面.把设计师从具体的UI编码中解放出来. Bootstrap提供了不少的前端UI组件.带下拉 ...
随机推荐
- 使用Gird++打印出现“Retrieving the COM class factory for component with CLSID”的解决办法
我们的接口需要返回一个gird++生成PDF文件的二进制数据,在本地测试都很好,发布到服务器上一直出现“Retrieving the COM class factory for component w ...
- FineUI添加隐藏标题
添加隐藏标题 窗体前台: <x:Button ID="btnShowHideHeader" runat="server" Icon="Secti ...
- Ubuntu 添加sudo用户
第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密 码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到 ...
- C++ list用法
创建一个list实例并赋值: // 创建实例以及赋值 #include <iostream> #include <list> using namespace std; int ...
- Heroku 部署时 time out 错误,对GFW无力吐槽!!!
整理自:http://ruby-china.org/topics/10813 部署到Heroku时输入 git push heroku master. 然后就开始漫长的等待了,最终报错: ssh: c ...
- TransactionScope的正确用法(转自:http://blog.csdn.net/greystar/article/details/1359960)
前一阵贴了一个关于transactionscope的贴子,以为是MS的BUG.后来经过认真仔细的调试,才找到原因. 原来的代码本身是没有没问题的.只是关于事务资源的处理放错了地方.正确的写法应该如下 ...
- zendguard安装破解
http://www.thefox.cn/zend-guard.shtml 到官网下载6.0版本安装后 URL: http://www.zend.com/en/products/guard/downl ...
- css透明度的一些兼容测试
前言 网站丢给了外包公司来弄,但是老外写css的时候似乎没有考虑到国内的浏览器市场,于是只用了opacity这个属性来写,当IE8-的浏览器访问的时候,浮动层就像一块大黑斑药膏贴在哪里.很显然,婀娜多 ...
- Android应用程序窗口(Activity)的窗口对象(Window) 的创建过程分析
每一个Activity组件都有一个关联的ContextImpl对象,同时,它还关联有一个Window对象,用来描述一个具体的应用程序窗口. 每一个Activity组件都有一个关联的ContextImp ...
- CCI_chapter 4 trees and Grapths
4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced ...