我对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 an int 和 not the actual name of the customer. How do I retrieve the names 和 not the id?

感谢你们所有人的帮助.对于这些基本的东西,我很抱歉.我还在学习.

推荐答案

通常,在创建发票时,您知道客户是谁,因此知道他们的ID.您可以将此信息分配给OnGet的发票:

public IActionResult OnGet()
{
    Invoice = new Invoice{ CustomerId = id };
    return Page();
}

接下来的问题是将该ID添加到发票表单,以便它与发票的其他详细信息一起过帐.您可以将其作为隐藏字段添加到表单中:

<input asp-for="Invoice.CustomerId" type="hidden" />

如果您不知道客户是谁,您可以提供一个包含客户的 Select 列表,以便用户可以为发票 Select 一个客户.将SELECT元素绑定到Invoice.CustomerId属性:

<select asp-for="Invoice.CustomerId" asp-items="Model.CustomerOptions"></select>

CustomerOptions是您在OnGet处理程序中填充的SelectList:

public SelectList CustomerOptions { get; set; }
public async Task<IActionResult> OnGet()
{
    CustomerOptions = new SelectList(await db.Customers.Select(x => new SelectListItem{
        Value = x.CustomerId.ToString(),
        Text = x.CustomerName
    }).ToListAsync(), nameof(SelectListItem.Value), nameof(SelectListItem.Text));
    return Page();
}

Csharp相关问答推荐

Microsoft.AspNetCore.Mvc. Controller Base.用户:属性或索引器Controller Base.用户无法分配给--它是只读的

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

Entity Framework Core 8 dbcontext—无法以多对多关系添加某些行'

迭代C#List并在数据库中 for each 元素执行SELECT语句—更有效的方式?

使用C#中的SDK在Microsoft Graph API上使用SubscribedSkus的问题

Razor视图Razor页面指向同一端点时的优先级

如何将DotNet Watch与发布配置和传递给应用程序的参数一起使用?

C#自定义验证属性未触发IsValid方法

等待一个等待函数

在implementationFactory中避免循环依赖

如何在microsoft.clearscript.v8的jsondata中使用Linq

为什么ReadOnlySpan;T&>没有Slice(...)的重载接受Range实例的?

JsonPath在Newtonsoft.Json';S实现中的赋值

我找不到ASP.NET Web应用程序(.NET框架).已 Select .NET框架项目和项模板以及此组件(&Q;)

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

从Base64转换为不同的字符串返回相同的结果

未显示详细信息的弹出对话框

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

WPF如何获取有关从一个视图模型更改另一个视图模型的信息

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果