MVC 4 map anonymous Data Entity
เกิดขึ้นในกรณีที่ จะ Add View ปรากฏว่า Field มันใช้แบบ multi ต้อง Add Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication5.Models { public class MyStudent { public Int32 id { get; set; } public String FirstName { get; set; } public String LastName { get; set; } public String FacultyName { get; set; } } } |
MVC 4 ViewBag รับและแสดงค่าในหน้าเว็บ
สามารถแสดงผลจาก object
1 2 3 4 5 6 7 |
public ActionResult Index() { List<String> s2 = new List<string>() { "a", "b", "c" }; ViewBag.MyData = "Hello"; ViewBag.MyData2 = s2; return View();//แบบแรก } |
ในส่วนของ View เรียกใช้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@{ ViewBag.Title = "Index"; } <h1>@ViewBag.MyData</h1> <h2>Index</h2> <ul> @foreach (String a in ViewBag.MyData2) { //เป็นการใช้ Razor <li>@a</li> } </ul> |
การค้อมเม้นต้องใส่
@*
comment code in mvc
*@
MVC 4.0 Map Route
- New Project MVC Application c#
- Right Click Controllers ->Add->Controller->Student
- Add View
ใส่โค้ดตามตัวอย่าง
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { public class StudentController : Controller { // // GET: /Student/ public ActionResult Index() { return View(); } public String Add() { return "Hello from Student-Add"; } public ActionResult arPartialView() { return PartialView("PartialView1"); } public ActionResult arRedirect() { return Redirect("http://www.google.co.th"); } public ActionResult arRedirectToAction() { return RedirectToAction("Index"); } public ActionResult arJason() { //not support cross domain List<String> a = new List<String>() { "a","b","c" }; return Json(a, JsonRequestBehavior.AllowGet); } public ActionResult arContent() { // return Content("<script>alert('Hello');</script>", "application/javascript");// content type return Content("<script>alert('Hello');</script>"); } public ActionResult arJavaScript() { return JavaScript("alert('Hello')"); } public ActionResult arResultDictToRoute() { // ข้าม controller return RedirectToRoute("default", new { Controller = "Home", Action = "index" }); } public ActionResult arFile() { return File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg", "image/jpeg"); } public String Delete(String StudentID) { return "Delete Student white ID=" + StudentID; //run http://localhost:1048/student/delete?studentid=11&id=5050 //auto parameter from query string } //http://localhost:1048/student/update/1 public String Update(Int32 id)// name id only { //if get keyid ex. http://localhost:1048/student/update/1 //only 1 parameter return "Update Student with ID=" + id; } //http://localhost:1048/Modify/2012/12345 public String Update2(String Year,String StudentID) { //return "Update Student: Year=" + Year + ",ID=" + StudentID; String r1 = "Update Student: Year=" + Year + ",ID=" + StudentID; String r2 = "Update Student: Year=" + Year; if (StudentID == null) { return r2; } else { return r1; } } } } |
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication1 { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Student", "student/modify/{year}/{studentid}", new {studentid=UrlParameter.Optional,//หมายถึงไม่มีก็ได้ // year=UrlParameter.Optional, Controller="Student",Action="Update2" } //year is optional ถ้าต้องการให้ Action อยู่บน parameter ให้เพิ่ม "student/{action}/{year}/{studentid}" //แล้วลบ Action="2" ออก ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } } |
ตรง map route สามารถเพิ่มเงื่อนไขในการตรวจสอบค่าที่รับมาได้โดยแก้ไขในไฟล์
1 2 3 4 5 6 7 8 9 10 11 |
routes.MapRoute( "Student", "student/modify/{year}/{studentid}", new { studentid = UrlParameter.Optional,//หมายถึงไม่มีก็ได้ // year=UrlParameter.Optional, Controller = "Student", Action = "Update2" }, new { year=@"\d{4}",studentid=@"\d{7}"} //Map Expression decimal number |
Recent Comments