2013-06-11 69 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