我有一份食物清单,我想制作一个“添加到膳食计划器”按钮,可以从我的餐桌(食物)中取出一定的食物并将其放入另一张桌子(膳食计划器)。
public ActionResult MealPlannerAliments(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MealPlanner mealPlannerAliment = db.MealPlanners.Find(id);
if (mealPlannerAliment == null)
{
return HttpNotFound();
}
return View(mealPlannerAliment);
}
我使用此代码(在控制器中)存储用户点击的ID ...但我不知道如何继续
答案 0 :(得分:1)
您可以在视图中简单地使用MealPlanner对象mealPlannerAliment并将其显示 在对象数据的帮助下,在另一个按钮事件上
答案 1 :(得分:1)
我不知道Aliment和MealPlanner类是如何建模的,但是这可以让你知道如何去做。
首先检查具有Id参数的Aliment是否有效,如果是,则检查MealPlanners是否已包含Aliment,如果没有,则填充MealPlanner对象并调用Add()和SaveChanges()方法;
public ActionResult MealPlannerAliments(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Aliment aliment = new Aliment();
aliment = db.Aliment.Find(id);
if (aliment == null)
{
return HttpNotFound();
}
else
{
MealPlanner mealPlannerAliment = db.MealPlanners.Find(aliment.id);
if (mealPlannerAliment == null)
{
mealPlannerAliment = aliment;
try{
db.Add(mealPlannerAliment);
db.saveChanges();
}
catch{
ViewBag.Error('Error! Try Later!')
}
}
else{
ViewBag.Error('this Aliment already exists in the Meal Planner')
}
}
return View();
}
答案 2 :(得分:-1)
您可以创建并使用HttpSession变量来存储这些选定的元素,然后在需要时显示到另一个表中。