【问题标题】:How to bind InputText to a model in razor如何将 InputText 绑定到 razor 中的模型
【发布时间】:2020-01-14 14:18:09
【问题描述】:

我有一个名为 Patient 的模型类,一个处理与 Patient 相关的服务的类,以及一个接口。除接口外,所有内容都是公共的,并且都在单独的文件中。当我尝试将 Razor 页面上的 InputText 绑定到患者属性时,ide 找不到它们。

剃须刀页面:

@using BlazorApp.Data;

<EditForm Model="@patient"> 
    <div class="col-12 row">
        <label class="col-2 font-weight-bold">first name: </label>
        <InputText class="form-control col-3" @bind-Value="patient.Name"/>
        placeholder="first name" />
    </div>
</EditForm>

@code {
public Patient patient {get;set;}
protected override void OnInitialized()
{
    patient =  new Patient();
}

}

模型类:

public class Patient
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Patient(){}

    public Patient(int id, string name, string lname, DateTime date)
    {
        ID = id;
        Name = name;
        LastName = lname;
        DateOfBirth = date;
    }
}

【问题讨论】:

  • 问题在于命名空间...在 _Imports.razor 文件中设置正确的引用
  • 将命名空间添加到导入文件中,问题仍然存在。 @enet
  • 这些类的命名空间是什么?您是否尝试在使用它们的组件中添加 using 指令?
  • 命名空间是 blazor,是的,它们也使用 using 指令添加。 @enet
  • 更改命名空间,不要再使用 blazor 这个词了。我不认为它是相关的,但你仍然不必定义这样的命名空间。现在,请将所有相关的命名空间和相关的 using 语句复制并粘贴到这里,以便我亲眼看到。

标签: c# asp.net-core razor blazor


【解决方案1】:

当我尝试将 Razor 页面上的 InputText 绑定到患者属性时,ide 找不到它们。

命名空间是 blazor,是的,它们也是使用 using 指令添加的。

我根据您的描述和代码进行了测试,这对我来说效果很好。请重新检查您的 Patient 类的命名空间,并确保您已通过 @using 指令导入它。

病人分类

namespace blazor 
{
    public class Patient
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public DateTime DateOfBirth { get; set; }
        public Patient() { }

        public Patient(int id, string name, string lname, DateTime date)
        {
            ID = id;
            Name = name;
            LastName = lname;
            DateOfBirth = date;
        }
    }
}

在 _Imports.razor

@using blazor

在组件中

<EditForm Model="@patient">
    <div class="col-12 row">
        <label class="col-2 font-weight-bold">first name: </label>
        <InputText class="form-control col-3" @bind-Value="patient.Name" placeholder="first name" />
    </div>
</EditForm>

@code{
    public Patient patient {get;set;}

    protected override void OnInitialized()
    {
        patient =  new Patient() {  Name = "test user"};
    }
}

测试结果

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2017-05-13
    • 2012-06-18
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2020-02-19
    相关资源
    最近更新 更多