简体   繁体   English

如何在剃刀中将 InputText 绑定到模型

[英]How to bind InputText to a model in razor

I have a model class called Patient, a class that handles the services related to Patient, and an interface.我有一个名为 Patient 的模型类,一个处理与 Patient 相关的服务的类和一个接口。 All are public except the interface and all are in separate files.除了接口之外,所有都是公共的,并且都在单独的文件中。 When i try to bind InputText on my Razor page to patient properties, the ide doesnt find them.当我尝试将 Razor 页面上的 InputText 绑定到患者属性时,ide 找不到它们。

The razor page:剃须刀页面:

@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();
}

} }

The model class:模型类:

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;
    }
}

When i try to bind InputText on my Razor page to patient properties, the ide doesnt find them.当我尝试将 Razor 页面上的 InputText 绑定到患者属性时,ide 找不到它们。

the namespace is blazor, and yes they are added with using directives as well.命名空间是 blazor,是的,它们也添加了 using 指令。

I did a test based on your description and code, which work well on my side.我根据你的描述和代码做了一个测试,这对我来说效果很好。 Please recheck the namespace of your Patient class and make sure you have imported it via @using directives.请重新检查您的Patient类的命名空间,并确保您已通过@using指令导入它。

Patient class病人班

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;
        }
    }
}

In _Imports.razor在 _Imports.razor

@using blazor

In component在组件中

<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"};
    }
}

Test Result测试结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM