我是ASP.NET核心剃刀页面的新手,我希望你能在以下方面提供帮助。
我有以下两个类:
public class Customer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage ="Please enter a name.")]
[Display(Name ="Customer Name")]
public string CustomerName { get; set; }
[Display(Name = "Customer Email")]
public string CustomerEmail { get; set; } = string.Empty;
[Display(Name ="Customer Phone Number")]
public string CustomerPhoneNumber { get; set; } = string.Empty;
public List<Invoice> Invoices { get; set; } = new();
}
和
public class Invoice
{
[Key]
public int Id { get; set; }
[Display(Name ="Invoice Number")]
public string InvNumber { get; set; }
[Display(Name = "Invoice Amount")]
public decimal InvAmount { get; set; }
[Display(Name = "Issued? (Yes/No)")]
public bool InvIssued { get; set; } = false;
[Display(Name = "Paid? (Yes/No)")]
public bool InvPaid { get; set; } = false;
public int CustomerId { get; set; }
}
我设置它的方式是一对多关系。现在,两个相关的问题是,在发票的Create
表单中,我还需要输入CustomerId
,否则我会得到SQL异常错误。但是,我一直不知道如何将数据绑定到Customer表,以便检索可用客户的列表,并将其作为创建发票期间的一个选项。
我的.cshtml
文件包含:
<div class="form-group">
<label asp-for="Invoice.CustomerId" class="control-label"></label>
<input asp-for="Invoice.CustomerId" class="form-control" value="3"/>
<span asp-validation-for="Invoice.InvNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Invoice.InvNumber" class="control-label"></label>
<input asp-for="Invoice.InvNumber" class="form-control" />
<span asp-validation-for="Invoice.InvNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Invoice.InvAmount" class="control-label"></label>
<input asp-for="Invoice.InvAmount" class="form-control" />
<span asp-validation-for="Invoice.InvAmount" class="text-danger"></span>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="InvIssued" id="InvIssued" value="true" checked="">
<label class="form-check-label" asp-for="Invoice.InvIssued">
The invoice has been issued.
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="InvNotIssed" id="InvNotIssed" value="false">
<label class="form-check-label" asp-for="Invoice.InvIssued">
The invoice has not been issued.
</label>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
我的页面模型是:
public class AddInvoiceModel : PageModel
{
private readonly InvoicesAppContext InvoiceContext;
public AddInvoiceModel(InvoicesAppContext contextInvoices)
{
InvoiceContext = contextInvoices;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Invoice Invoice { get; set; }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
InvoiceContext.Invoice.Add(Invoice);
await InvoiceContext.SaveChangesAsync();
return RedirectToPage("../Index");
}
}
所以我的问题是
- 如何在
AddInvoice
页面模型中添加对Customer表的引用? CustomerId
是int
,而不是客户的实际名称。如何检索名称而不是ID?
谢谢大家的帮助。对不起,这些基本的东西。我还在学习。
3条答案
按热度按时间8tntrjer1#
在AddInvoiceModel中:
在cshtml文件中:
结果:
gab6jxml2#
通常,在创建发票时,您知道客户是谁,因此知道他们的ID。您可以将其分配给
OnGet
中的发票:接下来的问题是将该ID添加到发票表单中,以便将其与发票的其余详细信息沿着发布。您可以将其作为隐藏字段添加到表单中:
如果您不知道客户是谁,则提供一个包含客户的选择列表,以便用户可以为发票选择一个。将select元素绑定到
Invoice.CustomerId
属性:CustomerOptions
是您在OnGet
处理程序中填充的SelectList
:af7jpaap3#
试试这个:
来源链接:https://www.learnrazorpages.com/razor-pages/model-binding