[英]ASP.Net Core MVC <select> <option> returns null
Working on an ASP.NET Core MVC application.处理 ASP.NET Core MVC 应用程序。 I have a <select><option>
with @model List<CatalogItem>
for my Razor view.我有一个带有@model List<CatalogItem>
的<select><option>
@model List<CatalogItem>
用于我的 Razor 视图。 I want to populate the form CartForm
with values from the CatalogItem
item.我想用CatalogItem
项中的值填充表单CartForm
。 When user selects an item and sends it back to action method asp-action="CartForm"
the selected item returns as null.当用户选择一个项目并将其发送回操作方法asp-action="CartForm"
,所选项目返回为 null。
What am I missing?我错过了什么?
<select id="itmSelect" name="itmSelect">
foreach (CatalogItem itm in Model)
{
<option class="text-dark" value="@itm">@itm.Description</option>
}
</select>
Although this does not explain the null
value:虽然这并不能解释null
值:
The value
of the <option>
must be a string, or at least a simple type. <option>
的value
必须是字符串,或者至少是简单类型。
Your @itm
is a complex type.你的@itm
是一个复杂的类型。
You should change it to something like:您应该将其更改为:
<option class="text-dark" value="@itm.ID">
By changing this, you at least post the correct selected value which might reflect your send model better.通过更改此设置,您至少可以发布正确的选定值,这可能会更好地反映您的发送模型。
As @Stefan says, the @itm is a complex type, we couldn't pass it to the controller directly in the select option value.正如@Stefan 所说,@itm 是一种复杂类型,我们无法将其直接在选择选项值中传递给控制器。
If you still want to pass the item based on the select option, I suggest you could try set some custom attribute for the select option with the CatalogItem property and add come hidden field on the view and the hidden input's name is the CatalogItem property name.如果您仍然想根据选择选项传递项目,我建议您可以尝试使用 CatalogItem 属性为选择选项设置一些自定义属性,并在视图上添加隐藏字段,隐藏输入的名称是 CatalogItem 属性名称。
Then you could use jquery to set the input hidden's value according to the dropdownlist select option.然后您可以使用 jquery 根据下拉列表选择选项设置输入隐藏的值。
The asp.net core model binding will bind the model according to the formdata's name, so it will bind well. asp.net核心模型绑定会根据formdata的名称绑定模型,所以绑定得很好。
More details, you could refer to below example.更多细节,你可以参考下面的例子。
CatalogItem model:目录项型号:
public class CatalogItem
{
public int Id { get; set; }
public string Description { get; set; }
public string Name { get; set; }
}
View:看法:
<form asp-action="DropdownGetValue" id="test1">
<select id="itmSelect" name="Id">
@foreach (var itm in Model)
{
<option class="text-dark" value="@itm.Id" Description="@itm.Description" Name="@itm.Name">@itm.Description</option>
}
</select>
<input type="hidden" name="Description" id="Description" />
<input type="hidden" name="Name" id="Name" />
<input type="submit" value="Click" />
</form>
Jquery script: jQuery 脚本:
<script type="text/javascript">
$(function () {
$("#itmSelect").change(function () {
$("#Description").val($('#itmSelect').find(":selected").attr("Description"));
$("#Name").val($('#itmSelect').find(":selected").attr("Name"));
});
})
</script>
Controller:控制器:
[HttpPost]
public IActionResult DropdownGetValue(CatalogItem itmSelect)
{
//_db.Category.Add(ca);
//_db.SaveChanges();
int i = 0;
return Ok();
}
Result:结果:
The problem I was having with the null value arose from the fact that I didn't name the parameter in the post-back method to match the name of the tag.我在使用 null 值时遇到的问题是因为我没有在 post-back 方法中命名参数以匹配标签的名称。 Once I got that into alignment everything worked.一旦我把它对齐,一切都奏效了。
Thanks to everyone who offered his/her time and comments.感谢所有提供时间和评论的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.