首页 > 解决方案 > 我想使用 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>
}

那是我想用来编辑我的成分的视图。有没有办法做到这一点?

标签: c#.netasp.net-mvcrazor

解决方案


推荐阅读