簡體   English   中英

相關數據未顯示asp.net C#

[英]Related data not showing asp.net c#

閱讀完本教程后, 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。

RecipeModel

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; }
}

RecipeLine

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; }
}

RecipeViewModel

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

Recipecontroller

 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值。

如果有人可以幫助解決這個問題,那就太好了。

您正在將item.RecipeIdViewBag.id比較,這不存在。 您在控制器操作中設置的ViewBag成員是ViewBag.RecipeId

但是,您根本不需要此條件。 所有配方行都已經有該配方ID,因為您專門在Model.RecipeLines設置了那些配方項。

//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>
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM