将 Html.DropDownList 的选定值传递给控制器

pass the selected value of Html.DropDownList to controller

我的视图页面中有一个下拉菜单。由于下拉菜单只有两个值且不会更改,因此我们决定创建静态下拉菜单。

@Html.DropDownList("RelationshipWithFather", new List<SelectListItem>()
 {
   new SelectListItem() { Text= "son", Value = "son" },
   new SelectListItem() { Text= "daughter", Value = "daughter" }
 }, "relationship...", new { @class = "form-control" })

如何将此 RelationshipWithFather 下拉菜单的选定值传递给创建控制器方法。

public ActionResult Create(PatientInfo vdm_)
        {
            if (ModelState.IsValid)
            {
                PatientInfo vdm = new PatientInfo();
                vdm.relationshipWithPatient = // selected value of RelationshipWithFather
            
            
            }
        }

我必须将所选下拉列表的值设置为模型 class 的 relationshipWithPatient 属性。

尝试使用 DropDownListFor

@model PatientInfo
....

@{
 var items= new List<SelectListItem>()
 {
   new SelectListItem() { Text= "son", Value = "son" },
   new SelectListItem() { Text= "daughter", Value = "daughter" }
 };
}

@using (Html.BeginForm()) {

@Html.DropDownListFor (model=>model.relationshipWithPatient, items , "relationship...", new { @class = "form-control" })

}

或者我更喜欢

<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
.....
   }
}