標籤:下拉 BMI success sharp comm 字元 tps rom code
這個問題可能能弱智,但是困擾了我好長時間,網上也一直搜不到,今天偶然一個機會在外國的論壇上看見這個。
首先,我想要實現的是自動綁定下拉選項,網上搜到很多方法,但是大家都是用的ViewBag(ViewData)將執行個體化好的DropDownList放在裡面然後傳遞到前台,我一開始也是這麼做的,但是後來莫名其妙的就出錯了,說是沒有執行個體化對象。
後來我有看了一些官方文檔,其中在ASP.NET Core 表單中的標記協助程式“https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.1#the-select-tag-helper”一章中,官方文檔給出了正確的綁定方式,先摘要如下:
Select Tag Helper asp-for 為 select 元素指定模型屬性名稱,asp-items 指定 option 元素。 例如:
HTML複製
<select asp-for="Country" asp-items="Model.Countries"></select>
樣本:
C#複製
using Microsoft.AspNetCore.Mvc.Rendering;using System.Collections.Generic;namespace FormsTagHelper.ViewModels{ public class CountryViewModel { public string Country { get; set; } public List<SelectListItem> Countries { get; } = new List<SelectListItem> { new SelectListItem { Value = "MX", Text = "Mexico" }, new SelectListItem { Value = "CA", Text = "Canada" }, new SelectListItem { Value = "US", Text = "USA" }, }; }}
Index 方法初始化 CountryViewModel,設定選定的省/地區並將其傳遞到 Index 視圖。
C#複製
public IActionResult IndexOption(int id){ var model = new CountryViewModel(); model.Country = "CA"; return View(model);}
HTTP POST Index 方法顯示選定內容:
C#複製
[HttpPost][ValidateAntiForgeryToken]public IActionResult Index(CountryViewModel model){ if (ModelState.IsValid) { var msg = model.Country + " selected"; return RedirectToAction("IndexSuccess", new { message = msg}); } // If we got this far, something failed; redisplay form. return View(model);}
Index 視圖:
CSHTML複製
@model CountryViewModel<form asp-controller="Home" asp-action="Index" method="post"> <select asp-for="Country" asp-items="Model.Countries"></select> <br /><button type="submit">Register</button></form>
產生以下 HTML(選擇“CA”時):
HTML複製
<form method="post" action="/"> <select id="Country" name="Country"> <option value="MX">Mexico</option> <option selected="selected" value="CA">Canada</option> <option value="US">USA</option> </select> <br /><button type="submit">Register</button> <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>" /> </form>
其中特別備忘了
不建議將 ViewBag 或 ViewData 與選擇標記協助程式配合使用。 視圖模型在提供 MVC 中繼資料方面更可靠且通常更不容易出現問題。
我才發現,其實正確的做法還是用模型還傳遞資料,我立馬照做了,但是在提交的時候又出現了新的問題,
InvalidOperationException: The entity type ‘SelectListGroup‘ requires a primary key to be defined.
後來百度後,在一個國外的網站上看到,說需要在模型屬性中加上【NotMapped】這個Attribute,官方解釋為:Denotes that a property or class should be excluded from database mapping.
如下:
[NotMapped] public List<SelectListItem> Countis { get; set; } = new List<SelectListItem>();
實驗後,果然就不出錯了,我理解其實出錯的原因在於,沒有加這個Attribute,它還想要通過Primary Key 往資料庫裡面插入Contis的選擇後資料,加上了之後,就沒有這個對應操作了。
回到標題,我一直想找一個Attribute,來描述一個欄位,這個欄位是我需要在Views中顯示,但是又不需要儲存在資料庫中的,最典型的例子就是在註冊使用者時候的“確認密碼”文字框,你需要它在Views中讓使用者來輸入,但是你肯定不會儲存一次密碼又儲存一次“確認密碼“,我一直沒找到對應的方法,在解決上面的問題後,我果斷的將【NotMapped】這個Attribute加到了我的確認密碼屬性上面,果然,重建的資料庫中沒有了確認密碼欄位。太棒了
[Display(Name = "確認密碼")] [Required(ErrorMessage = "您需要填寫{0}")] [StringLength(50, ErrorMessage = "{0}長度應在{2}至{1}個字元之間", MinimumLength = 6)] [Compare("Password",ErrorMessage ="{0}應和{1}一致")] [DataType(DataType.Password)] [NotMapped] public string ConfirmPassword { get; set; }
一直想要的,如何讓MVC模型不自動添加資料庫欄位