c# - 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
谢谢你的帮助。
解决方案
推荐阅读
- vba - 访问变量引用的另一种形式的 VBA 编辑文本框
- html - 为什么我使用 table-cell css 样式的页面在相同的宽度比率下显示不同的 div 宽度?
- android - 如何在 findNavController().navigate() 中发送带有方向的导航器附加信息
- asp.net-core - Serilog - can not log to multiple files based on property
- javascript - 遍历并删除对象数组中的元素
- r - 如何区分 R 中的“alist”和“list”?
- javascript - 如何在 Angular 应用程序中使用 BokehJS?
- reactjs - 如何使用 useEffect 从 Redux 获取更新的状态
- excel - 试图过滤包含“-M”的数据,但它给了我一个错误
- python - Docker 容器随机分段错误