简体   繁体   English

ASP.NET 核心 MVC<select><option>返回空值

[英]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.

相关问题 ASP.NET core 5 MVC Null 下拉列表选项 - ASP.NET core 5 MVC Null drop down list option ASP.NET 内核 Ajax 返回 null - ASP.NET Core Ajax returns null 为什么 IIS 服务器返回 ASP.NET CORE 5 MVC 项目的 null baseUrl,但在本地服务器上返回 true? - Why IIS Server returns null baseUrl of ASP.NET CORE 5 MVC project, but returns true on local server? ASP.NET MVC 5(非 MVC 核心)TempData 在 Azure AppService 环境之一中不起作用并返回 null 值 - ASP.NET MVC 5 (Not MVC Core) TempData is not working in one of the Azure AppService Environment and returns null value 在 Asp.Net Mvc Core 中调用存储库模式方法返回 null - Calling Repository Pattern methods returns null in Asp.Net Mvc Core User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application asp.net mvc 5 DropDownList nullable int默认选项不为null - asp.net mvc 5 DropDownList nullable int default option not null 如何在asp.net core中使用select2选项? - How to use the select2 option in asp.net core? GetExternalLoginInfoAsync在ASP.NET Core 2.0中返回null - GetExternalLoginInfoAsync returns null in asp.net core 2.0 ASP.NET Core:JSON 配置 GetSection 返回 null - ASP.NET Core: JSON Configuration GetSection returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM