Struggling to get a value from a drop down, to be passed into my controller for db inserting. It starts with a simple class for each item in the drop down.
public class selectListUrgencyNum
{
public string Text { get; set; }
public string Value { get; set; }
}
Then in my view model I create a list of these objects as such..
public class createViewModel
{
public Change Changevm { get; set; }
public List<RequestType> rTypes { get; set; }
public List<selectListUrgencyNum> listUrgency { get; set; }
}
Finally is my controller I populate this list to be displayed in the dropdown.
public ActionResult Create()
{
var urgNums = new List<selectListUrgencyNum>();
for(int i = 1; i < 6; i++)
{
urgNums.Add(new selectListUrgencyNum() { Text = i.ToString(), Value = i.ToString() });
}
var viewModel = new createViewModel
{
listUrgency = urgNums
};
return View(viewModel);
}
I just used 1 - 5 as to make it simpler to understand.
My view creates the ddl like this.
<div class="form-group">
@Html.LabelFor(model => model.Changevm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
<select class="form-control" id="select">
<option> 1 = Least Urgent</option>
@foreach(var item in Model.listUrgency)
{
<option>@item.Text</option>
}
</select>
@*How do I grab the value of what is selected??*@
@Html.ValidationMessageFor(model => model.Changevm.UrgencyNum, "", new { @class = "text-danger" })
</div>
So I'm not sure how to get the VALUE of what the selected drop down option is, so that on my insert I can access it. Possibly a hiddenfor?
And if I use a hidden for, how would it look?
If already answered please link..
<select>use@Html.DropDownListFor(m => m.Changevm.UrgencyNum, Model.listUrgency, ....)assuminglistUrgencyisIEnumerable<SelectListItem>which it should be.