You've given your
DropDownList
the name
"id"
. Therefore, you need to store the
SelectList
in
ViewBag.id
.
However, the code seems rather confused. You specify a
@model
for the view, but you only pass the model from the
HttpPost
method. In one method, you store a
List<TesterModel>
in the
ViewBag
, but in the other you store a
SelectList
. And you don't do anything with the submitted ID.
I'd be inclined to use the
HttpGet
method for both requests, since you're only sending a single
int
value:
[HttpGet]
public ActionResult Index(int? id = default)
{
var testers = db.Testers.Select(c => new { c.TesterID, c.Name }).ToList();
ViewBag.TesterList = testers.Select(c => new SelectListItem
{
Value = c.TesterID.ToString(),
Text = c.Name,
Selected = c.TesterID == id,
});
return View();
}
@using (Html.BeginForm("Index", "YourControllerName", FormMethod.Get))
{
@Html.DropDownList("id", (IEnumerable<SelectListItem>)ViewBag.TesterList, "-Select One-", new { @class = "form-control" })
<input type="submit" value="Search">
}