【问题标题】:Data that comes from database can not shown in drop down list in ASP.NET MVC来自数据库的数据不能显示在 ASP.NET MVC 的下拉列表中
【发布时间】:2021-06-03 22:50:33
【问题描述】:

我正在开发一个 ASP.NET MVC 项目。我正在尝试在添加和编辑页面上添加主详细信息。但是当我想编辑一条记录时,“纱线下拉列表”为空,但是当我调试项目数据时,项目数据正确来自数据库。我没有弄清楚这个问题。 Yarn 和 Yarn Detail 表之间的一对多关系,以及

//编辑页面

<table>
   <tr>
      <th>Yarn Name</th>
      <th>Unit Price of Yarn</th>
      <th>Percentage of Using</th>
      <th></th>
   </tr>
      @{ int i = 0;}
      @foreach (var x in Model.YarnDetails)
      {
                      
         <tr>
            <td>
               @Html.DropDownListFor(model => x.YarnID  , ViewBag.YarnList as SelectList, "--select--", new { @class = "form-control", @Name = "[" + i + "].YarnID" })
            </td>
                           
             <td>
                @Html.HiddenFor(model => x.YarnDetailID, new { @Name = "[" + i + "].YarnDetailID" })
                <input type="button" value="Remove" class="remove" />
             </td>
          </tr>
          i++;
      }
</table>

//控制器部分

public ActionResult Edit(int FabricCostCalcID)
    {

        var fabCo = _context.FabricCostCalcs.SingleOrDefault(c => c.FabricCostCalcID == FabricCostCalcID && c.IsDeleted == false);


        List<Fabric> fabriclist = _context.Fabrics.Where(x => x.IsDeleted == false).OrderBy(y => y.FabricName).ToList();
        List<KnittingOption> knittinglist = _context.KnittingOptions.Where(x => x.IsDeleted == false).OrderBy(y => y.KnittingName).ToList();
        List<Yarn> yarnList = _context.Yarns.Where(x => x.IsDeleted == false).OrderBy(y => y.YarnName).ToList();

        ViewBag.YarnList = new SelectList(yarnList, "YarnID", "YarnName");
        ViewBag.FabricList = new SelectList(fabriclist, "FabricID", "FabricName");
        ViewBag.KnitingList = new SelectList(knittinglist, "KnittingOptionID", "KnittingName");

        return PartialView("_EditPartial", fabCo);
    }

 [HttpPost]
public ActionResult Edit(FabricCostCalc fabricCost, List<YarnDetail> yarnDetails) 
    {

 
        _context.Entry(fabricCost).State = EntityState.Modified;

        foreach (var yar in yarnDetails)
        {

            yar.FabricCostCalcID = fabricCost.FabricCostCalcID;
           
            _context.Entry(yar).State = EntityState.Modified;
        }
        _context.SaveChanges();

        return RedirectToAction("Index");
    }

//前端屏幕截图

//DATABESE

提前谢谢你

【问题讨论】:

    标签: asp.net-mvc entity-framework razor master-detail


    【解决方案1】:

    我明白你想要做什么。如果你这样做会更好:

    @for (int i = 0; i < Model.LSubCategories.Count(); i++)
    {
                           <div class="col">
                                <label asp-for="SubCategory.CategoryID">Bulunacağı Kategori ?</label>
                                  <div class="col">
                            <label asp-for="@Model.LSubCategories[i].CategoryID">Kategorisini seçiniz ?</label>
                            <select asp-for="@Model.LSubCategories[i].CategoryID" class="selectpicker" data-width="100%" name="CategoryID" required>
                                <option value="">Seçiniz</option>
                                @foreach (var item in Model.LSubCategories)
                                {
                                    <option value="@item.CategoryID">@item.CategoryName</option>
                                }
                            </select>
    }
    

    我希望你能理解我的代码。 如果你意识到我在不使用 @Html.DropDownListFor 的情况下做同样的事情。 如果你想调用相关数据,你可以在你的 ViewModel 中定义它。之后,您可以将其填写在您的控制器中:

     var list = _unitOfWorkSCat.RepositorySubCategory.GetAll();
                var listCat = _unitOfWorkCat.RepositoryCategory.GetAll();
                var listL = _unitOfWorkSCat.RepositorySubCategory.GetAllAsList();
                var SubCategory = new SubCategory();
                var CategoryViewModel = new CategoryViewModel
                {
                    SubCategories = list,
                    SubCategory = SubCategory,
                    LSubCategories = listL,
                    Categories = listCat
                };
    

    【讨论】:

    • 其实这不是我想做的。我在我的“面料计算”中添加一种或多种纱线,就像在订单表中可以包含一种或多种产品一样。然后我计算价格并保存。但是,当我要编辑记录时,保存的数据不会显示在编辑页面中,而是其他数据来了。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    相关资源
    最近更新 更多