2013-06-11 137 views
0

我想有這關係到一個對象+對象的財產清單相關手柄修改

前一種形式:名稱和每個x項目:是否激活/價格

注:我知道項目的數量,它與另一個表有關。所有數據都可以由用戶編輯。

我該如何用ASP .Net MVC實現這一目標?

public class AddChargeModel 
{ 
    [Required] 
    [Display(Name = "Name")] 
    public string Nom { get; set; } 
    [Required] 
    public List<ChargeLotModel> ChargeLot; 
} 

public class ChargeLotModel 
{ 
    [Required] 
    [Display(Name = "IdLot")] 
    public int IdLot { get; set; } 
    [Display(Name = "Price")] 
    public decimal Price { get; set; } 
    [Display(Name = "Active")] 
    public bool Active { get; set; } 
} 

我類AddChargeModel我的觀點聯繫起來:

當我點擊該按鈕,然後在我的控制器功能輸入:

[HttpPost] 
public ActionResult Add(AddChargeModel model) 
{ 
    ... 
} 

model.Name充滿但不是model.ChargeLot == null。

數組中的控件在網頁上被命名爲ChargeLot_0__Price。 (如果我很好理解,它應該可以工作)

您是否有解決方案使其工作?

+0

這是在這個線程討論,一起來看看:http://stackoverflow.com/questions/11267354/how-to-produce-non-sequential-prefix-collection-indices-with -mvc-html-editor-tem/11267659#11267659 – MiBu

回答

2

您的ChargeLot「屬性」沒有getter或setter,因此模型聯編程序無法使用發佈的數據填充它。它只是一個標準的實例變量而不是一個屬性,並且您的模型中沒有設置它。您需要改爲:

public List<ChargeLotModel> ChargeLot { get; set; } 
+0

不僅如此,問題是由於索引而綁定列表。他需要爲列表中的每個項目的每個屬性都有不同的Id/-name。用一些mvvm js框架很容易實現,否則他需要做一些事情,像http://stackoverflow.com/questions/11267354/how-to-produce-non-sequential-prefix-collection-indices-with-mvc-html -editor-tem/11267659#11267659 – MiBu

+0

@MiBu:不可以。表單輸入已被命名爲'ChargeLot_0__Price'。這是模型活頁夾期待關係的格式。 OP的視圖代碼很好。 –

+0

我不是100%確定它是否正確...不應該是AddChargeModel.ChargeLot而不是ChargeLot? – MiBu