My list can insert 2 type of records into database: Type and Text. Now the records in my database is like:
ID StoreID Type Text
1 1 Human a
2 1 Human b
3 1 Animal c
How to combine texts and use comma separated if they have same type? I my database should look like this:
ID StoreID Type Text
1 1 Human a,b
2 1 Animal c
Here's some of my codes for Respirators table:
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());
}
}
and in my Chemical controller, I have:
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");
}
In 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))
In Symptom page:
@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")
</p>
</div>
When I click on the "+", another div will be created. I also check if div id is "Animal", then the Symptom_Type will be changed to Animal. Now I have no problem to store all data into database, but the problem is how to combine records with same Symptom_Type?