列表框发布但不填充模型
Listbox POSTing but not populating model
我有一个看起来像这样的模型:
public List<SelectListItem> Interests { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "Buyers Agent", Text = "Buyers Agent" },
new SelectListItem { Value = "Showing Agent", Text = "Showing Agent" },
new SelectListItem { Value = "Realtor Associate", Text = "Realtor Associate" },
new SelectListItem { Value = "Transaction Coordinator", Text = "Transaction Coordinator" },
new SelectListItem { Value = "Guest Blog Writer", Text = "Guest Blog Writer" }
};
视图如下所示:
<div>
<label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
<select asp-for="Interests" asp-items="Model.Interests"></select>
</div>
并呈现为:
<div>
<label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
<select id="Interests" multiple="multiple" name="Interests">
<option value="Buyers Agent">Buyers Agent</option>
<option value="Showing Agent">Showing Agent</option>
<option value="Realtor Associate">Realtor Associate</option>
<option value="Transaction Coordinator">Transaction Coordinator</option>
<option value="Guest Blog Writer">Guest Blog Writer</option>
</select>
</div>
如果项目被选中,它们将被回发,但控制器调用在模型的 'Interests' 属性 中显示 0 个项目。控制器看起来像这样:
[HttpGet]
[AllowAnonymous]
public IActionResult Interest()
{
var model = new InterestFormViewModel();
return View(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult Interest(InterestFormViewModel model)
{
// A breakpoint and watch show model.Interests to have a count of 0.
return View("HRThanks");
}
我一定是忽略了一些明显的东西,有人能告诉我它可能是什么吗?谢谢!
<select>
元素返回单个值(或在 [=15= 的情况下返回一组简单值)。您不能将 select 绑定到 Interests
因为它是复杂对象的集合 (SelectListItem
)。
您的模型需要一个 属性 来绑定 selected 值。如果你想要一个下拉列表,那么(比如)
public string SelectedItem { get; set; }
并在视图中
<label for="SelectedItem" ...</label>
<select asp-for="SelectedItem" asp-items="Model.Interests"></select>
或者如果你想要一个列表框,那么
public IEnumerable<string> SelectedItems { get; set; }
并在视图中
<select asp-for="SelectedItems" asp-items="Model.Interests"></select>
请注意,在将模型传递给视图之前,您在 GET 方法中设置了 SelectedItem
(或 SelectedItems
)的值,并且其值与选项之一匹配, 那么该选项最初将被 selected。
我有一个看起来像这样的模型:
public List<SelectListItem> Interests { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "Buyers Agent", Text = "Buyers Agent" },
new SelectListItem { Value = "Showing Agent", Text = "Showing Agent" },
new SelectListItem { Value = "Realtor Associate", Text = "Realtor Associate" },
new SelectListItem { Value = "Transaction Coordinator", Text = "Transaction Coordinator" },
new SelectListItem { Value = "Guest Blog Writer", Text = "Guest Blog Writer" }
};
视图如下所示:
<div>
<label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
<select asp-for="Interests" asp-items="Model.Interests"></select>
</div>
并呈现为:
<div>
<label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
<select id="Interests" multiple="multiple" name="Interests">
<option value="Buyers Agent">Buyers Agent</option>
<option value="Showing Agent">Showing Agent</option>
<option value="Realtor Associate">Realtor Associate</option>
<option value="Transaction Coordinator">Transaction Coordinator</option>
<option value="Guest Blog Writer">Guest Blog Writer</option>
</select>
</div>
如果项目被选中,它们将被回发,但控制器调用在模型的 'Interests' 属性 中显示 0 个项目。控制器看起来像这样:
[HttpGet]
[AllowAnonymous]
public IActionResult Interest()
{
var model = new InterestFormViewModel();
return View(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult Interest(InterestFormViewModel model)
{
// A breakpoint and watch show model.Interests to have a count of 0.
return View("HRThanks");
}
我一定是忽略了一些明显的东西,有人能告诉我它可能是什么吗?谢谢!
<select>
元素返回单个值(或在 [=15= 的情况下返回一组简单值)。您不能将 select 绑定到 Interests
因为它是复杂对象的集合 (SelectListItem
)。
您的模型需要一个 属性 来绑定 selected 值。如果你想要一个下拉列表,那么(比如)
public string SelectedItem { get; set; }
并在视图中
<label for="SelectedItem" ...</label>
<select asp-for="SelectedItem" asp-items="Model.Interests"></select>
或者如果你想要一个列表框,那么
public IEnumerable<string> SelectedItems { get; set; }
并在视图中
<select asp-for="SelectedItems" asp-items="Model.Interests"></select>
请注意,在将模型传递给视图之前,您在 GET 方法中设置了 SelectedItem
(或 SelectedItems
)的值,并且其值与选项之一匹配, 那么该选项最初将被 selected。