2017-06-22 145 views
0

我有,我想綁定到選擇列表不張貼選擇選項控制器

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

你的問題是在這裏:

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