I have a drop down in my view page. Since the drop down has only two values and it will not change so we decided to create static drop down.
@Html.DropDownList("RelationshipWithFather", new List<SelectListItem>()
{
new SelectListItem() { Text= "son", Value = "son" },
new SelectListItem() { Text= "daughter", Value = "daughter" }
}, "relationship...", new { @class = "form-control" })
How to pass the selected value of this RelationshipWithFather
drop down to create controller method.
public ActionResult Create(PatientInfo vdm_)
{
if (ModelState.IsValid)
{
PatientInfo vdm = new PatientInfo();
vdm.relationshipWithPatient = // selected value of RelationshipWithFather
}
}
I have to set the value of the selected dropdown to the relationshipWithPatient
attribute of the model class.
CodePudding user response:
Try to use 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" })
}
or I prefer
<select class="form-control" asp-for="@Model.relationshipWithPatient" asp-items="@items"></select>
action
public ActionResult Create(PatientInfo vdm)
{
if (ModelState.IsValid)
{
var selected= vdm.relationshipWithPatient; //selected value of RelationshipWithFather
.....
}
}