我试图将数据插入到一个有外键的表中。我已经将类别放在一个select标记中,并正确显示它们,但当我试图将该ID字符串传递给SelectedSubCatCategory
时,它显示为null。
<select @bind="SelectedSubCatCategory">
@foreach (var item in category_items)
{
<option value="@item.Cat_ID" >@item.Cat_Name </option>
}
</select>
string SelectedSubCatCategory;
protected async Task SubCategoryInsert()
{
Guid guid = Guid.NewGuid();
string SubCatGUID = guid.ToString();
var category = await categoryService.GetEntryByIdAsync(SelectedSubCatCategory);
SubCategory sc = new SubCategory()
{
SCat_ID = SubCatGUID,
SCat_Cat = category,
SCat_Name = SelectedSubCatName
};
await subCategoryService.InsertEntryAsync(sc);
}
我自己想好了:
你必须有一个@onchange事件,而不是@bind
<select @onchange="SetSelectedSubCatCategory">
<option> Please select </option> // default option
@foreach (var item in category_items)
{
<option value="@item.Cat_ID" >@item.Cat_Name </option>
}
</select>
然后这个小代码将完成剩下的
void SetSelectedSubCatCategory(ChangeEventArgs e)
{
SelectedSubCatCategory = e.Value.ToString();
}
ez