I'm having an issue using the DropDownListFor()
function with ASP.NET MVC:
Model:
public class ShoeSizeModel
{
public List<ShoeModel> ShoeListModel { get; set; }
public List<SizeModel> Sizes { get; set; }
public int SelectedSizeId { get; set; }
public IEnumerable<SelectListItem> SizesSelectList
{
get
{
return new SelectList(Sizes, "SizeId", "Size");
}
}
View:
@model Models.ShoeSizeModel
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shoe Selection</title>
<link rel="stylesheet" href="../../Scripts/css/base.css">
<link rel="stylesheet" href="../../Scripts/css/gallery.css">
</head>
<body>
@Html.AntiForgeryToken()
@using (Html.BeginForm("FilterBySize", "Shoe", FormMethod.Post))
{
<div class="refiners">
@Html.DropDownListFor(
model => model.SelectedSizeId,
Model.SizesSelectList,
"Size",
new { onchange = "this.form.submit();" })
</div>
}
...
Controller:
[HttpPost]
public ActionResult FilterBySize(int sizeId)
{
return View();
}
If I try to pass the SizeId from the SelectList to the FilterBySize method, I get a null value:
Interestingly, if I try to pass it as a model instead, the select list value passes just fine:
I'm probably just missing something obvious here, and it's not the end of the world if I just have to pass the model and take the value from there, but I'd rather just pass the value if I can. Please can someone explain to me what I'm doing wrong?
int? sizeId
toint selectedSizeId
, because you named it that way in your model. - David Liang