通常だとSQL Serverなどが定番だが、手軽に利用できるmdbを使ってみた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System.Collections.Generic; using System.Web.Mvc; using System.Data.OleDb; namespace WebApplication2.Controllers { public class HomeController : Controller { public ActionResult Test() { List<object> obj = new List<object>(); obj.Add(new { id = 2, name = "taro" }); obj.Add(new { id = 3, name = Request.Params["serial"] }); return Json(obj); } public ActionResult Index() { List<string> list = new List<string>(); using (OleDbConnection con = new OleDbConnection()) { con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./") + @"\dat.mdb" + ""; con.Open(); OleDbCommand cmd = new OleDbCommand("select * from tbl",con); using (OleDbDataReader dr =cmd.ExecuteReader()) { while (dr.Read()) { list.Add(dr["serial"].ToString()); } } } return View(list); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
@model List<string> @foreach (string item in Model) { @item; } <form action="" method="post"> AAA: <input type="text" id="txt" value="AAA" /><br /> BBB: <input type="text" id="txt" value="BBB" /> <input type="button" id="btn" value="送信" /> </form> <script> $(document).on("click", "#btn", function () { SayHello(""); }); function SayHello(msg) { var serial = $("#txt").val(); $.ajax({ type: "POST", url: "/Home/Test", acync: false, data: { serial: serial }, dataType: 'json', success: function (json) { for (var i = 0; i < json.length; i++){ alert(json[i].name); } alert(msg); }, error: function () { alert("NG"); } }); } </script> |