【发布时间】:2018-10-31 03:17:10
【问题描述】:
当用户提交表单时,我试图从我的数据库中获取多个对象的 ID 和名称。不过,当它显示结果时,我不希望有任何重复。
我有我的表格:
@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" />
}
}
这是使用 HttpPost 方法发送到我的搜索控制器的:
[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();
}
有没有更好的方法将 ID 和名称添加到 ViewBag?我知道我现在所做的并不是最佳做法。
【问题讨论】:
标签: c# asp.net asp.net-mvc linq viewbag