c# - 我想使用 DropDownListFor 来编辑我的食谱的成分,所以我需要将 ICollection 项目映射到 DropDownListFor
问题描述
我正在使用 EF,表之间的关系是多对多的。我想显示和编辑 ICollection 属性,不仅可以显示我的食谱中的成分,还可以编辑、添加或删除它们。
我尝试使用EditorFor,但成分的更改从未更改并提交到数据库。我想使用 DropDownList,因为它可以以列表格式显示我的食谱中的成分,以便我可以在它们之间进行选择。
这是我的配方和配料模型,带有关系表RecipesIngredients:
namespace Licenta.Models
{
public class Recipe
{
[Key]
public int IDRecipe { get; set; }
public string Name { get; set; }
public string Desc { get; set; }
public string Steps { get; set; }
public float Kcal { get; set; }
public float Pro { get; set; }
public float Carbo { get; set; }
public float Fat { get; set; }
public virtual ICollection<RecipesIngredients> RecipesIngredients { get; set; }
}
}
namespace Licenta.Models
{
public class RecipesIngredients
{
[Key]
[Column(Order = 1)]
public int IDRecipe { get; set; }
[Key]
[Column(Order = 2)]
public int IDIngredient { get; set; }
public virtual Recipe Recipe { get; set; }
public virtual Ingredient Ingredient { get; set; }
}
}
namespace Licenta.Models
{
public class Ingredient
{
[Key]
public int IDIngredient { get; set; }
public string Nume { get; set; }
public float Kcal { get; set; }
public float Pro { get; set; }
public float Carbo { get; set; }
public float Fat { get; set; }
public virtual ICollection<RecipesIngredients> RecipesIngredients { get; set; }
}
}
这是我的控制器:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Recipe recipe = db.Recipes.Find(id);
if (recipe == null)
{
return HttpNotFound();
}
return View(recipe);
}
// POST: Recipes/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "IDRecipe,Name,Kcal,Pro,Carbo,Fat,Desc,Steps,Ingredients,RecipesIngredients")] Recipe recipe)
{
if (ModelState.IsValid)
{
db.Entry(recipe).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(recipe);
}
以及编辑页面的视图:
@model Licenta.Models.Recipe
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Rețetă</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.IDRecipe)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@foreach (var item in Model.RecipesIngredients)
{
<td>
@Html.DropDownListFor(model => item.Ingredient.Nume, @* this is where i want to edit the ingredients*@)
</td>
}
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.RecipesIngredients, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
那是我想用来编辑我的成分的视图。有没有办法做到这一点?
解决方案
推荐阅读
- python-3.x - 在 MacOSX Mavericks 上使用 PyQt5、Python3 和 Anaconda3 出现问题
- angular - 如何在 Angular 4 中包含 canvas-datagrid
- android - 检查小米是否应该振动来电
- react-native - react-native-router-flux:没有为 key undefined 定义路由。必须是 'home'、'activeJobs'、'favorites'、'inbox'、'profile' 之一
- javascript - 为什么动态添加的视图输入字段无法在主视图上附加 jQuery 事件
- vb.net - 根据 DB 结果 VB.net 更改文本框中的数据
- c# - 优化以下正则表达式
- spring - Spring-boot:日期查询参数字符串格式
- sql - MS Access / SQL Server JOIN 以返回左表中的所有值,如果在右表中缺失则返回零
- backbone.js - BackboneJS,保存方法被触发两次