C#MVC列表项的集合

时间:2015-01-08 21:17:15

标签: c# asp.net-mvc entity-framework list model-view-controller

我有一个项目,我需要有两个项目列表A,B,C,D& 1,2,3,4等这个列表将有一个CRUD接口来随时间更改列表(通常只添加到列表中)。

然后在页面上我有一个数据条目,最终用户将在其旁边看到两个带有CheckBoxes的列表,以便他们可以选择该列表中的哪一个适用于该条目。因此,他们可以从第一个列表A,D和第二个列表中选择。我需要将此选择与页面上的其他条目一起存储,如名称,注释,区域等。

我的问题是,使用MVC和EF我在数据库中有一个模型和表以及两个列表的CRUD,我甚至设计了我的数据输入页面,但是当点击保存按钮时我不确定如何最好地存储和保存这些数据。

我不确定我是否完全描述了这一点,但我需要一个列表,其中包含每个列表的可能选择,然后在输入数据时将条目的选择保存回DB。

我希望它有意义,如果我有代码,我会发布,但我仍然处于设计阶段,所以我所拥有的是废话,因此使用了大删除键,我开始新鲜,所以寻找想法。

由于

悬崖。

[编辑]

我想我想说的是我想在页面上有两个多选列表,并存储这两个选项。但是列表中的值也来自数据库。

[编辑]

好的,好像我找到了一个可以处理列表的插件,实际上非常适合我的需求

MvcCheckBoxList

只需要弄清楚如何在单个视图中使用其中两个列表,这应该不会太困难。

{编辑}

Ok正在努力解决这个问题并将以下内容作为我的模型......

 public class CallResult
{
    [Key]
    public int CallResultId { get; set; }
    public string Area { get; set; }
    public string Number { get; set; }
    public DateTime Date { get; set; }
    public ICollection<TargetCustomer> TargetCustomers { get; set; }

}

public class TargetCustomer
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    public virtual ICollection<CallResult> CallResults { get; set; }
}

我需要一个带有多个TargetCustomers选择的创建页面,然后用户选择的内容将作为CallResult中列表的一部分存储。

到目前为止,我的创建动作是:

public ActionResult Create()
    {
        var targetCustomers = new List<TargetCustomer>()
        {
            new TargetCustomer() { Id = 1, Name = "Manger", CallResults = new Collection<CallResult>()},
            new TargetCustomer() { Id = 2, Name = "Worker", CallResults = new Collection<CallResult>()},
        };


        ViewBag.MultiSelectTC = new MultiSelectList(targetCustomers, "Id", "Name");
        return View();
    }

View中的代码段:

    <div class="form-group">
        <div class="control-label col-md-2">
            <label>Target Customer</label>
        </div>
        <div class="col-md-10">
            @Html.ListBox("targetCustomers", (MultiSelectList)ViewBag.MultiSelectTC)
        </div>
    </div>

现在我只需要将数据恢复到Action中,这样它就可以存储在EF数据库中的CallResult中了,我有这个:

public ActionResult Create([Bind(Include = "CallResultId,Area,Number,Date")] CallResult callResult, int[] targetCustomers)
    {
        if (ModelState.IsValid)
        {
            // Find Tag from Database
            // Attach tag entity to Post

            foreach (var custId in targetCustomers)
            {
                var cust = db.TargetCustomer.Find(custId); 
                callResult.TargetCustomers.Add(cust);
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(callResult);
    }

但是当我到达callResult.TargetCustomers.Add(cust)时,我得到Null Ref的例外;线。

我希望这一切比昨晚我的随意更有意义并感谢你的帮助...

1 个答案:

答案 0 :(得分:1)

取决于数据,有很多方法。但最后你想要的是表关系。 1个用户可以有多个选项。一个选项可以是多个用户。

为简单起见:

Table `option_list_1`:
id - option_name
1 - a
2 - b

Table `option_list_2`:
id - option_name
1 - 1
2 - 2

Table `user_data`:
user_data_id - name - notes

继承:

Table `user_options`:
user_data_id - discriminator - option_id

非继承:

Table `user_options_1`
user_data_id - option_id

Table `user_options_2`
user_data_id - option_id