I am trying to get the ID and name of multiple objects from my database when a user submits a form. I don't want any repeats though when it displays the results.
I have my Form:
@using (Html.BeginForm("Advancedresults", "Search", FormMethod.Post))
{
{
foreach (var r in ViewBag.res)
{
string hes = r.iname;
<div class="col-md-4">
@Html.CheckBox("drink", false, new { value = hes })
@r.iname
</div>
}
<input type="submit" />
}
}
This is sent to my Search Controller using the HttpPost Method:
[HttpPost]
public ActionResult Advancedresults()
{
string drinks = Request.Form["drink"];
List<string> dList = drinks.Split(',').ToList();
dList = dList.Where(x => x != "false").ToList();
List<string> r = new List<string>();
foreach (string st in dList)
{
r.AddRange(from a in db.Recipes.AsEnumerable()
join b in db.RecipeIngredients on a.ID equals b.recipe_id
join i in db.Ingredients on b.ingredient_id equals i.id
join u in db.Measures on b.measure_id equals u.id
where i.name.Equals(st)
select a.name
);
}
List<string> ra = new List<string>();
foreach (string ras in r)
{
if (!ra.Contains(ras))
{
ra.Add(ras);
};
}
ViewBag.Ingredients = ra.ToList();
return View();
}
Is there a better way of adding the ID and name to the ViewBag? I know what I am doing now is not best practice.