首页 > 解决方案 > Blazor 服务器 - 将项目插入另一个表时插入 CustomerID

问题描述

我的项目在这里有几个层次。客户 -> 项目

项目表具有 CustomerID 的值。我正在使用我的 CustomersModel 获取 CustomerNames 列表。

我在这里尝试做的简单示例。从列表中选择一个客户,然后输入一个新的项目名称并将其提交到一个表中。绑定 CustomerID 会导致“选择客户”选项不会显示在选择下拉列表中。如果我将 CustomerName 设置为绑定值,它将显示为下拉菜单的占位符。

但是我需要使用新的项目名称将 CustomerID 传递给 Projects 表。

我不确定我在这里缺少什么。

--

组件代码

                                        <EditForm Model="@newProject" OnValidSubmit="@InsertLocation">
                    <DataAnnotationsValidator />
                    <div class="form-group">
                        <div class="row">
                            <div class="col-md-5">
                                <label class="form-label">Customer Name</label>
                                <InputSelect class="form-control" id="CustomerName" @bind-Value="newProject.CustomerID">
                                @if (customer is null)
                                        {

                                        }
                                        else
                                        {
                                        <option selected disabled value="-1"> Choose a Customer</option>
                                        @foreach (var customer in customers)
                                            {
                                            <option>@customer.CustomerName</option>
                                            }
                                        }
                                </InputSelect>
                                @*<ValidationMessage For="(() => newProject.CustomerID)" />*@
                            </div>
                            <div class="col-md-5">
                                <label class="form-label">Location Name</label>
                                <InputText class="form-control" id="ProjectName" @bind-Value="newProject.ProjectName" placeholder="Location Name" />
                                <ValidationMessage For="(() => newProject.ProjectName)" />
                            </div>
                        </div>

来自组件的@code

    private ProjectsViewModel newProject = new ProjectsViewModel();
    private async Task InsertLocation()
{

    //assign the new project data to the project model in the back end
    ProjectsModel project = new ProjectsModel
    {
        //TODO: make these match what is in the model and update insert query
        ProjectName = newProject.ProjectName,
        CustomerID = newProject.CustomerID

    };

    await projectsData.InsertLocation(project); //send data to db
    newProject = new ProjectsViewModel();
    projects = await projectsData.ReadProjects();
    OpenAddLocationModal = false;

}

最后从 projectData 文件发送到存储过程。

        public async Task InsertLocation(IProjectsModel project)
    {
        var p = new
        {
            project.CustomerID,
            project.ProjectName
        };
        await _db.SaveData("dbo.spProject_Create", p, "DefaultConnection");
    }

SQL 过程

@ProjectName nvarchar(255),
@CustomerID int

as

begin
insert into dbo.Projects(CustomerID, ProjectName)
values (@CustomerID, @ProjectName);
end

谢谢你的帮助。

标签: c#sqlinsertblazor

解决方案


推荐阅读