【问题标题】:Related data not showing asp.net c#相关数据未显示asp.net c#
【发布时间】:2015-10-13 15:11:48
【问题描述】:

阅读本教程http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application 后,我创建了一些模型、控制器和视图。

食谱在视图中显示得很好,但我无法让 RecipeLines 显示。

配方模型

public class RecipeModel
{
    [Key]
    public int RecipeId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public  string ImageUrl { get; set; }
    public virtual ICollection<RecipeLine> RecipeLines { get; set; }
}

配方行

public class RecipeLine
{
    [Key]
    public int RecipeLineId { get; set; }
    public int RecipeId { get; set; }
    public double Quantity { get; set; }
    public UnitOfMeasureModel UnitOfMeasure { get; set; }
    public IngredientModel Ingredient { get; set; }
}

配方视图模型

public class RecipeViewModel
{
    public IEnumerable<RecipeModel> RecipeModels { get; set; }
    public IEnumerable<RecipeLine> RecipeLines { get; set; }
}

配方控制器

 public class RecipeController : Controller
    {
        private RecipeApplicationDb db = new RecipeApplicationDb();

        [HttpGet]
        public ActionResult Index(int? id)
        {
            var viewModel = new RecipeViewModel();
            viewModel.RecipeModels = db.Recipes
                //.Include(i => i.Name)
                .Include(i => i.RecipeLines);

            if (id != null)
                {
                ViewBag.RecipeId = id.Value;
                viewModel.RecipeLines = viewModel.RecipeModels.Where(i => i.RecipeId == id.Value).Single().RecipeLines;
                }

            return View(viewModel);
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RecipeModel recipeModel = db.Recipes.Find(id);
            if (recipeModel == null)
            {
                return HttpNotFound();
            }
            return View(recipeModel);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

还有风景

@model RecipeApplication.Models.RecipeViewModel

@{
    ViewBag.Title = "Recepten";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            Naam
        </th>
        <th>
            Omschrijving
        </th>
        <th>
            Afbeelding
        </th>
    </tr>

@foreach (var item in Model.RecipeModels) {
    string selectedRow = "";
    if(item.RecipeId == ViewBag.RecipeId)
    {
        selectedRow = "success";
    }
    <tr class="@selectedRow" valign="top">
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @if (item.ImageUrl != null)
            {
                @Html.DisplayFor(modelItem => item.ImageUrl)
            }
        </td>
        <td>
            @Html.ActionLink("Select", "Index", new { id = item.RecipeId}) |
            @Html.ActionLink("Edit", "Edit", new { id=item.RecipeId }) |
            @Html.ActionLink("Details", "Details", new { id=item.RecipeId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RecipeId })
        </td>
    </tr>
}
</table>

@if (Model.RecipeLines != null)
{
    foreach (var item in Model.RecipeLines)
    {
        string selectedRow = "";
        if (item.RecipeId == ViewBag.id)
        {
            <p>@item.Quantity @item.UnitOfMeasure @item.Ingredient</p>
        }
    }
}

在选择配方时,该行确实得到了正确的颜色,并且我在 URL 字符串中看到了一个 id 值。

如果有人能帮忙解决这个问题,那就太棒了。

【问题讨论】:

    标签: c# asp.net-mvc related-content


    【解决方案1】:

    您将item.RecipeIdViewBag.id 进行比较,这并不存在。您在控制器操作中设置的ViewBag 成员是ViewBag.RecipeId

    但是,您根本不需要这个条件。所有配方行都已用于该配方 ID,因为您仅在 Model.RecipeLines 中专门设置了那些配方项。

    【讨论】:

      【解决方案2】:
      //change your controller action
      [HttpGet]
      public ActionResult Index(int? id)
      {
          if(id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      
          var model = new RecipeViewModel();
          var data  = db.RecipeModel.Include(i => i.RecipeLines)
                          .Where(x=>x.RecipeId == id)
                          .ToList();
      
          model.RecipeModels = data;
          return View(model);
      }
      
      //change your viewModel
      public class RecipeViewModel
      {
          public IEnumerable<RecipeModel> RecipeModels { get; set; }
      }
      
      
      //this is in the view
      @if (Model.RecipeLines != null)
      {
          foreach (var item in Model.RecipeModels.RecipeLines)
          {
              <p>
                  @item.Quantity 
                  @item.UnitOfMeasure 
                  @item.Ingredient
              </p>
          }
      }
      

      【讨论】:

      • 所以基本上你说的是你不知道这是如何工作的......谷歌导航属性......例如“public virtual ICollection RecipeLines { get; set; }” find在示例代码中......那么......你认为“db.Recipes.Include(i => i.RecipeLines)”有什么作用?很抱歉,您对框架缺乏了解令人担忧。
      • @JamieD77 不,它有“RecipeModels”,它有一个 EF 导航到“RecipeLines”,这是框架的工作原理。
      • @JamieD77 按原样检查其正确性,我不知道如何接受您的最后评论。是的,如果这是问题的话,它会起作用。做了 1 次更正,不知道你说的是不是这个?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2022-08-22
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多