我对ASP.NET Core Razor页面非常陌生,我希望在以下方面得到您的帮助.
我有以下两节课:
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; }
}
The way I've set it up is for a one-to-many relationship. Now, two related issues are that during the Create
form for an invoice I also need to input a CustomerId
, otherwise I get an SQL exception error. However, for the life of me, I can't figure out how to bind the data, to the Customer table, so that I can retrieve the list of available customers 和 have it as an option during the creation of an invoice.
我的.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>
和 my page model is:
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表的引用? - The
CustomerId
is anint
和 not the actual name of the customer. How do I retrieve the names 和 not the id?
感谢你们所有人的帮助.对于这些基本的东西,我很抱歉.我还在学习.