我在我的視圖頁面中有一個下拉選單。由于這個下拉選單只有兩個值,而且它不會改變,所以我們決定創建靜態下拉選單。
@Html.DropDownList("RelationshipWithFather", new List< SelectListItem> ()
{
new SelectListItem() { Text= "son", Value = "son" },
new SelectListItem() { Text= "daughter", Value = "daughter" }.
}, "關系...", new { @class = "表單-控制" })
如何將這個RelationshipWithFather下拉的選擇值傳遞給創建控制器方法。
public ActionResult Create(PatientInfo vdm_)
{
if (ModelState.IsValid)
{
PatientInfo vdm = new PatientInfo()。
vdm.relationshipWithPatient = //選擇RelationshipWithFather的值。
}
}
我必須將選定的下拉值設定為模型類的relationshipWithPatient屬性。
uj5u.com熱心網友回復:
嘗試使用DropDownListFor
。@model PatientInfo
....
@{
var items= new List<SelectListItem> ()
{
new SelectListItem() { Text= "son", Value = "son" },
new SelectListItem() { Text= "女兒", Value = "女兒" }.
};
}
@using (Html.BeginForm()) {
@Html.DropDownListFor (model=>model.relationshipWithPatient, items , "關系...", new { @class = "表單控制" })
}
或者我更喜歡
< select class="form-control" asp-for="@model. relationshipWithPatient" asp-items="@items"> </select>
行動
public ActionResult Create(PatientInfo vdm)
{
if (ModelState.IsValid)
{
var selected= vdm.relationshipWithPatient; //selected value of RelationshipWithFather。
.....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322717.html
標籤:
上一篇:如何設定帶有狀態代碼的自定義例外
