如何将列表中的所有记录转换为2条记录?

时间:2015-12-10 22:12:35

标签: c# asp.net asp.net-mvc

我的列表可以在数据库中插入2种类型的记录:Type和Text。 现在我的数据库中的记录如下:

ID   StoreID   Type        Text
1       1      Human        a
2       1      Human        b
3       1      Animal       c

如果它们具有相同的类型,如何组合文本并使用逗号分隔? 我的数据库应该是这样的:

ID   StoreID   Type        Text
1       1      Human        a,b
2       1      Animal       c

以下是我的呼吸器表代码:

public NPG_Chemical()
{
    this.NPG_Symptoms = new HashSet<NPG_Symptom>();
}
public virtual ICollection<NPG_Symptom> NPG_Symptoms { get; set; }
internal void CreateSymptom(int count = 1)
{
    for (int i = 0; i < count; i++)
    {
        NPG_Symptoms.Add(new NPG_Symptom());
    }
}

在我的化学控制器中,我有:

public ActionResult Create()
{
    var nPG_Chemical = new NPG_Chemical();
    nPG_Chemical.CreateSymptoms(1);
    return View(nPG_Chemical);
}
[HttpPost]
public ActionResult Create(NPG_Chemical nPG_Chemical)
{
    db.NPG_Chemical.Add(nPG_Chemical);
    db.SaveChanges();
    return Redirect("Create");
}

在Chemical.cshtml中:

<p>
Symptom Type
</p>
<div id="humans">
<label>
    Human
</label>
@Html.EditorFor(model => model.NPG_Chemical_Symptoms)
</div>
@Html.AddLink("+", "#humans", ".symptom", "NPG_Chemical_Symptoms", typeof(NPG_Administrative_Utility.Models.NPG_Chemical_Symptom))
<div id="animals">
<label>
    Animal
</label>
</div>
@Html.AddLink("+", "#animals", ".symptom", "NPG_Chemical_Symptoms", typeof(NPG_Administrative_Utility.Models.NPG_Chemical_Symptom))

在症状页面中:

@model NPG_Administrative_Utility.Models.NPG_Chemical_Symptom

<div class="symptom" style="display:inline-block;">
<p>
    @Html.RemoveLink("x", "div.symptom", "input.mark-for-delete")
    @Html.HiddenFor(x => x.DeleteSymptom, new { @class = "mark-for-delete" })
    @Html.TextBoxFor(x => x.Symptom_Text)
    @Html.Hidden("Symptom_Type", "Human")
    &nbsp;&nbsp;&nbsp;
</p>
</div>

当我点击“+”时,将创建另一个div。我还检查div id是否为“Animal”,然后Symptom_Type将更改为Animal。现在我将所有数据存储到数据库中都没有问题,但问题是如何将记录与相同的Symptom_Type组合在一起?

0 个答案:

没有答案