选择列表不向控制器发布选定选项

时间:2017-06-22 11:27:58

标签: c# asp.net-mvc selectlist

我有一个当前的发票清单模型,我想绑定到选择清单

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);
}

任何人都可以看到我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

public Invoice SelectedInvoice{ get; set; }
@Html.DropDownListFor(m => m.SelectedInvoice

Html.DropDownListFor期望第一个参数属于选项的value类型。
因此,如果您的invoiceReferenceint,那么您可以这样做:

public class InvoiceList
{
   public string InvoicerReference { get; set; }
   public int SelectedInvoice{ get; set; }
   public List<Invoice> Invoices { get; set; }
}