如何将复选框选项列表映射到模型第一类?

时间:2013-05-02 15:00:06

标签: asp.net-mvc

我的网络表单中有以下选项列表:

enter image description here

我想先与模特合作。但我非常喜欢MVC和Model-first,所以我不知道如何在我的模型类中更好地表示它。

我应该为每个项目制作一个属性吗?或者我应该创建一个数组,其中每个位置是一个选项(可能使用enums)?

public bool[] Comportamento { get; set; }
// or
public Comportamento[] Comportamento { get; set; }
// or
public bool Manso { get; set; }
public bool Arisco { get; set; }
...

1 个答案:

答案 0 :(得分:0)

你应该有一个类CompartamentoViewModel,如下所示:

public class CompartamentoViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }
}

然后,在主视图模型中:

public class MyViewModel
{
    // List of all objects for the view
    public List<CompartamentoViewModel> Compartamentos { get; set; }

    // List of ints to retrieve selected values
    public List<int> SelectedCompartamentos { get; set; }
}

在视图中,使用文本的描述和值的ID。在帖子中,使用所选ID列表填充SelectedCompartamentos

免责声明:我不知道如何复数“Compartamento。”

相关问题