# MVC 查詢功能(選單查詢) ### index.cshtml * 新增一個name="abc"的清單列 > <form> @Html.DropDownList("abc") <input type="submit" value="提交"/> </form> > ### xxxController.cs 1. index帶入變數值 > public ActionResult Index(String abc){...} > 2. 取得資料庫資料 > ViewBag.abc = new SelectList(資料集合,"索引(value)","顯示(text)"); > 3. 過濾條件 > if (!string.IsNullOrEmpty(abc)) { 資料集合 = 資料集合.Where (x => x.資料欄位名稱.ToString() == abc).ToList(); } > 4. 更改前台顯示畫面 > return View(資料集合); > ### 完整程式碼 > public ActionResult Index(String abc){ ViewBag.abc = new SelectList(資料集合,"索引(value)","顯示(text)"); if (!string.IsNullOrEmpty(abc)) { 資料集合 = 資料集合.Where (x => x.資料欄位名稱.ToString() == abc).ToList(); } return View(資料集合); } >
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up