ASP.Net MVC Razor HTML Help 範例

 

  1. @{
  2. ViewBag.Title = "RazorHelp";
  3. }
  4.  
  5. <h2>RazorHelp</h2>
  6.  
  7. <h3>文字框(input type="text")</h3>
  8.  
  9. @{
  10. string defaultValue = "Default Value";
  11. }
  12.  
  13. <h5>有預設值</h5>
  14. @Html.TextBox("textBoxName", defaultValue, new { @class = "form-control" })
  15.  
  16. <h5>預設值為空</h5>
  17.  
  18. @Html.TextBox("textBoxName", null, new { @class = "form-control" })
  19. <hr />
  20.  
  21. <h3>下拉選單(select)</h3>
  22.  
  23. <h5>有預設值</h5>
  24. @{
  25. List<SelectListItem> items = new List<SelectListItem>
  26. {
  27. new SelectListItem { Text = "Option 1", Value = "1" },
  28. new SelectListItem { Text = "Option 2", Value = "2", Selected = true },
  29. new SelectListItem { Text = "Option 3", Value = "3" }
  30. };
  31. }
  32.  
  33. @Html.DropDownList("dropdownName", items, "Select an option", new { @class = "form-control" })
  34.  
  35. <h5>預設值為空</h5>
  36.  
  37. @{
  38. List<SelectListItem> items2 = new List<SelectListItem>
  39. {
  40. new SelectListItem { Text = "Option 1", Value = "1" },
  41. new SelectListItem { Text = "Option 2", Value = "2" },
  42. new SelectListItem { Text = "Option 3", Value = "3" }
  43. };
  44. }
  45.  
  46. @Html.DropDownList("dropdownName", items2, "Select an option", new { @class = "form-control" })
  47.  
  48. <hr />
  49.  
  50. <h3>checkbox</h3>
  51.  
  52. @{
  53. bool isChecked = true; // 設置是否選中
  54. }
  55.  
  56. <h5>有預設值</h5>
  57.  
  58. @Html.CheckBox("checkBoxName", isChecked, new { @class = "form-check-input" })
  59.  
  60. <h5>預設值為空</h5>
  61.  
  62. @Html.CheckBox("checkBoxName", false, new { @class = "form-check-input" })
  63.  
  64. <hr />
  65.  
  66. <h3>radio button</h3>
  67.  
  68. @{
  69. string selectedValue2 = "option2"; // 選擇預設值
  70. }
  71.  
  72. <h5>有預設值</h5>
  73.  
  74. <label>
  75. @Html.RadioButton("radioButtonName", "option1", selectedValue2 == "option1") Option 1
  76. </label>
  77.  
  78. <label>
  79. @Html.RadioButton("radioButtonName", "option2", selectedValue2 == "option2") Option 2
  80. </label>
  81.  
  82. <label>
  83. @Html.RadioButton("radioButtonName", "option3", selectedValue2 == "option3") Option 3
  84. </label>
  85.  
  86.  
  87. <h5>預設值為空</h5>
  88.  
  89. <label>
  90. @Html.RadioButton("radioButtonName", "option1") Option 1
  91. </label>
  92.  
  93. <label>
  94. @Html.RadioButton("radioButtonName", "option2") Option 2
  95. </label>
  96.  
  97. <label>
  98. @Html.RadioButton("radioButtonName", "option3") Option 3
  99. </label>
  100.  
  101. <hr />
  102.  
  103. <h3>輸出資料包含HTML</h3>
  104.  
  105. @{
  106. string htmlStr = "123<br/>456";
  107. }
  108.  
  109.  
  110. <h5>沒處理, 直接輸出文字</h5>
  111.  
  112. <div>@htmlStr</div>
  113.  
  114.  
  115. <h5>html.raw()處理, 輸出HTML格式</h5>
  116.  
  117. <div>@Html.Raw(@htmlStr)</div>
  118.  
  119. <hr />

留言

這個網誌中的熱門文章

ORA-12514: TNS: 監聽器目前不知道連線描述區中要求的服務

Oracle 工作排程 DBMS_JOB 筆記

Oracle 例外控制(Exception Control)