asp.net-core - asp.net 核心创建/更新外键
问题描述
我在我的“Todo”课程中添加了我的“Pharmacy”课程的外键。当我添加或编辑待办事项时,无法链接任何药房(从选择框)。这是我的模型:
public class Todo
{
[Key]
public int ID { get; set; }
public Pharmacy Pharmacy { get; set; }
}
public class Pharmacy
{
[Key]
public int PharmacyID { get; set; }
public string Name { get; set; }
public string Pharmacist { get; set; }
[DataType(DataType.PhoneNumber)]
public int Phone { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public int NPA { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string Canton { get; set; }
}
我的代码列表列出了我的 Todo 上的所有药房:
public class PharmacynamePageModel : PageModel
{
public SelectList PharmacyNameSL { get; set; }
public void PopulatePharmacysDropDownList(ApplicationDbContext _context, object selectedPharmacy = null)
{
var query = (from p in _context.Pharmacy orderby p.Name select p).ToList();
PharmacyNameSL = new SelectList(query, "PharmacyID", "Name", selectedPharmacy);
}
}
好吧,在这一步,我的 HTML 表单上有这段代码:
<label asp-for="Todo.Pharmacy" class="control-label"></label>
<select asp-for="Todo.Pharmacy" class="form-control"
asp-items="Model.PharmacyNameSL">
<option value="">-- Select a Pharmacy --</option>
</select>
<span asp-validation-for="Todo.Pharmacy" class="text-danger" />
和我的 PostAsync() 进入我的创建和编辑控制器:
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var emptyTask = new Todo();
if (await TryUpdateModelAsync(emptyTask, "Todo",
s => s.ID, s => s.OwnerID,
s => s.Title, s => s.Description,
s => s.DueDate,
s => s.Pharmacy,
s => s.Priority))
{
emptyTask.Status = EnumStatus.Open;
_context.Todo.Add(emptyTask);
await _context.SaveChangesAsync();
if (emptyTask.OwnerID != HttpContext.User.Identity.Name)
{
LogVM.AddLog("Todos", $"{HttpContext.User.Identity.Name} attributed a new task to {emptyTask.OwnerID}.", _context);
}
return RedirectToPage("./Index");
}
PopulatePharmacysDropDownList(_context, emptyTask.Pharmacy);
return RedirectToPage("./Index");
}
所有数据都被映射/绑定,但不是药房字段,其值始终为空。
希望你能帮我 :)。
感谢提前。
解决方案
在发布时,数据绑定会将 Select 的“dataValueField”绑定到模型的数据成员。
您正在尝试将整个对象映射到 Select 的选定项目。您提到只有 Pharmacy 字段为空 - 这可能是因为从您提供的代码看来,这是您可能尝试修改的唯一字段。其他项目的值可能是在页面代码本身中设置的(并且不是在 Post 上绑定的数据)。
我建议您为选定的“药房”项目创建一个单独的字段并将其绑定到“选择”框。
在您的 PharmacynamePageModel 类中,创建一个公共“可绑定”属性,如下所示:
[BindProperty]
public string SelectedPharmacy { get; set; }
然后在 Razor 代码上,不是绑定到“Pharmacy”,而是绑定到“SelectedPharmacy”(在 asp-for 属性中),如下所示:
<select asp-for="SelectedPharmacy" class="form-control"
asp-items="Model.PharmacyNameSL">
<option value="">-- Select a Pharmacy --</option>
</select>
<span asp-validation-for="Todo.Pharmacy" class="text-danger" />
现在,在 Form post 上,所选的 Pharmacy Id 应该可用。您可以根据需要使用 ID。
希望这有效。如果是,请将此标记为答案。
推荐阅读
- java - 从 Maven 配置中设置 VM 选项
- python - 如何计算另一组的平均值?
- json - 从“获取”到“显示”JSON API 数据的转变 Swift Node.js
- .net - 使用 Azure App Config -> 希望允许本地配置覆盖开发人员
- mysql - sql 不能用数字作为列名插入
- javascript - 如果两个条件之一为真,如何以角度隐藏元素
- h2 - 点燃 2.10.0 org.h2.jdbc.JdbcSQLException:找不到函数“LOCK_MODE”
- node.js - 写入文件节点js时页面无限重新加载
- node.js - Process.stdout.write 将“true”附加到控制台输出(再次)
- flutter - 添加 Flutter Web:如何解决未创建 Firebase 应用“[DEFAULT]”