我有一个当前的发票清单模型,我想绑定到选择清单
public class InvoiceList
{
public string InvoicerReference { get; set; }
public Invoice SelectedInvoice{ get; set; }
public List<Invoice> Invoices { get; set; }
}
以下是视图中的下拉列表
@using (Html.BeginForm("Invoice", "Billing", FormMethod.Post))
{
@Html.DropDownListFor(m => m.SelectedInvoice, new SelectList(Model.Invoices, "invoiceReference", "InvoiceDisplay"))
<input type="submit" id="btnSelectInvoice" />
}
然而,当我发布到我的控制器时,模型为空
public ActionResult Invoice(InvoiceList invoiceReference)
{
....
return View(invoiceList);
}
任何人都可以看到我做错了什么吗?
答案 0 :(得分:0)
你的问题在这里:
public Invoice SelectedInvoice{ get; set; }
@Html.DropDownListFor(m => m.SelectedInvoice
Html.DropDownListFor
期望第一个参数属于选项的value
类型。
因此,如果您的invoiceReference
是int
,那么您可以这样做:
public class InvoiceList
{
public string InvoicerReference { get; set; }
public int SelectedInvoice{ get; set; }
public List<Invoice> Invoices { get; set; }
}