我正在try 将模型中的CountryId绑定到Blazor中选定项SelectList的值.所有的国家/地区项目都出现在一个类似{CountryId, CountryName} Object的列表中.我这样做代码:

    <InputSelect @bind-Value="model.ByCountryId" class="form-control">
        @if (model?.Countries != null)
        {
           @foreach (var cnt in model.Countries)
           {
               <option value="@cnt.Id">@cnt.Name</option>
           }
        }
     </InputSelect>

和挡路代码:

@code {

BrandModel model = new BrandModel();

protected override async Task OnInitializedAsync()
{
    model = new BrandModel
    {
        Id = 19,
        ByCountryId = 1,
        Countries = new List<ent.Country>
            {
                new ent.Country { Id = 1, Name = "Azerbaijan" },
                new ent.Country { Id = 2, Name = "Turkey" }
            },
        IsActive = true,
        Name = "Brand"
    };
}

但是这个执行在浏览器中给了我一个错误:

blazor.webAssemy.js:1 WASM:System.MissingMethodException: 找不到"System.ComponentModel.ByteConverter"类型的构造函数.

在Blazor中绑定<select>model.data的便捷方式是什么?

推荐答案

当我将<InputSelect>放入<EditForm Model="@model">..</EditForm >中时,它工作得很好,并且您的数据绑定没有问题.

try 使用以下代码在csproj文件中设置<BlazorLinkOnBuild>false</BlazorLinkOnBuild>.

<PropertyGroup>
   <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>

参考https://github.com/aspnet/AspNetCore/issues/7784

Update:

使用<select>标签而不是像这样的<InputSelect>标签

<select @bind="model.ByCountryId">
        @if (model?.Countries != null)
        {
            @foreach (var cnt in model.Countries)
            {
                <option value="@cnt.Id">@cnt.Name</option>
            }
        }
</select>

Asp.net相关问答推荐

威胁已经被清除了

解析器错误消息:无法加载类型sometype

ASP.NET MVC4 jquery/javascript 包的使用

由于缺少定义,在 .net Core 应用程序上构建失败

Twitter bootstrap glyphicons 不会出现在发布模式 404 中

发布发布事件

如何使用自动生成的列隐藏 ASP.NET GridView 中的列?

排序下拉列表? - C#,ASP.NET

如何在asp.net中获取应用程序路径?

在asp.net mvc 3中实现FilterAttribute,IActionFilter和从ActionFilterAttribute继承有什么区别?

带有 ASP.NET WebMethod 的 Jquery AJAX 返回整个页面

解析器错误消息:无法加载类型网络营销

HttpResponse.End 或 HttpResponse.Close 与 HttpResponse.SuppressContent

什么时候应该使用 Response.Redirect(url, true)?

如何进入 IIS 管理器?

elmah:没有 HttpContext 的异常?

如何使用 jquery 设置单选按钮 Select 的值

将 html 标记从 jquery 发布调用发送到 asp.net 页面时,从客户端检测到潜在危险的 Request.QueryString 值

配置授权服务器端点

.Net 上是否有 URL 验证器?