首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blazor字节数据类型的服务器端InputSelect,onChange事件错误

Blazor字节数据类型的服务器端InputSelect,onChange事件错误
EN

Stack Overflow用户
提问于 2019-12-27 17:06:48
回答 1查看 1.6K关注 0票数 3

我使用的是Blazor服务器端的ASP.NET Core3.1。我关注https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1上的文档

代码语言:javascript
复制
@page "/add_fileset"
@inject Foo.Data.ApplicationDbContext Db

<div>
    <a href="/fileset">Danh sách</a>
</div>

<div>
    <EditForm Model="@model" OnValidSubmit="@HandleValidSubmit">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <div class="form-group row">
            <label for="FileName" class="col-sm-2 col-form-label">Tên File: </label>
            <div class="col-sm-10">
                <input type="text" id="FileName" @bind-value="model.FileName" class="form-control" />
            </div>
        </div>
        <div class="form-group row">
            <label for="PriorityLevel" class="col-sm-2 col-form-label">Mức ưu tiên: </label>
            <div class="col-sm-10">
                @*<input type="number" id="PriorityLevel" @bind-value="model.PriorityLevel" class="form-control" />*@
                <InputSelect id="PriorityLevel" @bind-Value="model.PriorityLevel" class="form-control">
                    <option>Chọn...</option>
                    <option value=1>Rất quan trọng</option>
                    <option value=2>Quan trọng</option>
                    <option value=3>Bình thường</option>
                    <option value=4>Ít quan trọng</option>
                </InputSelect>
            </div>
        </div>

        <div class="form-group row">
            <label for="StatusCode" class="col-sm-2 col-form-label">Mã trạng thái: </label>
            <div class="col-sm-10">
                <input type="number" id="StatusCode" @bind-value="model.StatusCode" class="form-control" />
            </div>
        </div>

        @*<div class="form-group row">
            <label for="StatusCode2" class="col-sm-2 col-form-label">Loại hồ sơ: </label>
            <div class="col-sm-10">
                <input type="number" id="StatusCode2" class="form-control" />
            </div>
        </div>*@

        <button type="submit" class="btn btn-primary">Lưu</button>
    </EditForm>
</div>


@code {
    public class AddFiletSetPageModel
    {
        public string FileName { get; set; }
        public decimal? CreatorId { get; set; }
        public DateTime? Created { get; set; }
        public decimal? ModifierId { get; set; }
        public DateTime? Modified { get; set; }
        public byte? PriorityLevel { get; set; }
        public byte? StatusCode { get; set; }
    }

    private AddFiletSetPageModel model = new AddFiletSetPageModel();

    private void HandleValidSubmit()
    {
        FileSet fileItem = new FileSet
        {
            FileName = model.FileName,
            CreatorId = model.CreatorId,
            Created = DateTime.Now,
            PriorityLevel = model.PriorityLevel,
            StatusCode = model.StatusCode
        };
        Db.FileSet.Add(fileItem);
        Db.SaveChanges();
    }
}

请关注以下代码行

代码语言:javascript
复制
<InputSelect id="PriorityLevel" @bind-Value="model.PriorityLevel" class="form-control">
    <option>Chọn...</option>
    <option value=1>Rất quan trọng</option>
    <option value=2>Quan trọng</option>
    <option value=3>Bình thường</option>
    <option value=4>Ít quan trọng</option>
</InputSelect>

单击下拉列表时,控制台出现错误

代码语言:javascript
复制
[2019-12-27T08:58:22.819Z] Error: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputSelect`1[System.Nullable`1[System.Byte]] does not support the type 'System.Nullable`1[System.Byte]'.

   at Microsoft.AspNetCore.Components.Forms.InputSelect`1.TryParseValueFromString(String value, TValue& result, String& validationErrorMessage)

   at Microsoft.AspNetCore.Components.Forms.InputBase`1.set_CurrentValueAsString(String value)

   at Microsoft.AspNetCore.Components.Forms.InputSelect`1.<BuildRenderTree>b__4_0(String __value)

   at Microsoft.AspNetCore.Components.EventCallbackFactoryBinderExtensions.<>c__DisplayClass22_0`1.<CreateBinderCore>b__0(ChangeEventArgs e)

--- End of stack trace from previous location where exception was thrown ---

   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)

   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) blazor.server.js:15:27309
    log https://localhost:44348/_framework/blazor.server.js:15
    C https://localhost:44348/_framework/blazor.server.js:8
    S https://localhost:44348/_framework/blazor.server.js:8
    invokeClientMethod https://localhost:44348/_framework/blazor.server.js:1
    invokeClientMethod https://localhost:44348/_framework/blazor.server.js:1
    processIncomingData https://localhost:44348/_framework/blazor.server.js:1
    onreceive https://localhost:44348/_framework/blazor.server.js:1
    onmessage https://localhost:44348/_framework/blazor.server.js:1
    (Async: EventHandlerNonNull)
    connect https://localhost:44348/_framework/blazor.server.js:1
    connect https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    L https://localhost:44348/_framework/blazor.server.js:1
    L https://localhost:44348/_framework/blazor.server.js:1
    connect https://localhost:44348/_framework/blazor.server.js:1
    startTransport https://localhost:44348/_framework/blazor.server.js:1
    createTransport https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    createTransport https://localhost:44348/_framework/blazor.server.js:1
    startInternal https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    a https://localhost:44348/_framework/blazor.server.js:1
    (Async: promise callback)
    c https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    startInternal https://localhost:44348/_framework/blazor.server.js:1
    start https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    B https://localhost:44348/_framework/blazor.server.js:1
    start https://localhost:44348/_framework/blazor.server.js:1
    startInternal https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    v https://localhost:44348/_framework/blazor.server.js:1
    v https://localhost:44348/_framework/blazor.server.js:1
    startInternal https://localhost:44348/_framework/blazor.server.js:1
    startWithStateTransitions https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    s https://localhost:44348/_framework/blazor.server.js:1
    v https://localhost:44348/_framework/blazor.server.js:1
    v https://localhost:44348/_framework/blazor.server.js:1
    startWithStateTransitions https://localhost:44348/_framework/blazor.server.js:1
    start https://localhost:44348/_framework/blazor.server.js:1
    S https://localhost:44348/_framework/blazor.server.js:8
    s https://localhost:44348/_framework/blazor.server.js:8
    s https://localhost:44348/_framework/blazor.server.js:8
    r https://localhost:44348/_framework/blazor.server.js:8
    r https://localhost:44348/_framework/blazor.server.js:8
    S https://localhost:44348/_framework/blazor.server.js:8
    E https://localhost:44348/_framework/blazor.server.js:8
    s https://localhost:44348/_framework/blazor.server.js:8
    s https://localhost:44348/_framework/blazor.server.js:8
    r https://localhost:44348/_framework/blazor.server.js:8
    r https://localhost:44348/_framework/blazor.server.js:8
    E https://localhost:44348/_framework/blazor.server.js:8
    <anonymous> https://localhost:44348/_framework/blazor.server.js:8
    n https://localhost:44348/_framework/blazor.server.js:1
    <anonymous> https://localhost:44348/_framework/blazor.server.js:1
    <anonymous> https://localhost:44348/_framework/blazor.server.js:1

如何修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-27 17:59:36

当用户选择像"<option ...value=1 ...>"这样的选项时,InputSelect的值是string "1"而不是int 1。这就是它抛出的原因。

一个快速的解决方法是将PriorityLevel的类型更改为string

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59498364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档