Modeling a many-to-many relationship in ASP.NET MVC using LINQ to SQL

前端 未结 6 1738
时光说笑
时光说笑 2020-12-14 05:15

I\'ve been reading and watching videos about MVC and I\'m starting to get a little confused. The simple tutorials are far too simple and they seem to only involve a single d

6条回答
  •  孤城傲影
    2020-12-14 05:27

    I have many examples of this type of relationship in my current project. I'm using MVC 1 and LINQ-to-SQL. I went through exactly the same frustration then as you are experiencing now. The biggest hurdle is accepting the fact that LINQ-to-SQL doesn't directly manage many-to-many relationships, but understanding that it doesn't need to in order to get the information you require.

    Let's start with the R in CRUD, since it's the easiest way to demonstrate what needs to be done. Now, at this point I would recommend creating a strongly-typed view model, just to ease the task of aggregating your view data and to simplify the assignment of the meal data to the Details view:

    public class MealDetailsViewModel
    {
        public int Id_Meal { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
    
        private List _Ingredients = new List();
        public List Ingredients
        {
           get { return _Ingredients; }
           set { _Ingredients = value; }
        }
    }
    

    To retrieve a meal and its list of ingredients, here's the controller method:

    public ActionResult Details (int mealID)
    {
        Meal result = DataContext.Meals
            .Where(a => a.Id_Meal == mealID)
            .SingleOrDefault();
    
        MealDetailsViewModel viewModel = new MealDetailsViewModel
        {
            Id_Meal = result.Id,
            Title = result.Title,
            Description = result.Description,
            Ingredients = result.Meals-Ingredients
                .Where(a => a.Id_Meal == mealID)
                .Select(a => a.Ingredient)
                .ToList()
        };
    
        return View(viewModel);
    }
    

    As previously stated, LINQ-to-SQL doesn't directly support many-to-many relationships, which is why you cannot see the Ingredients entity directly from Meal. However, as illustrated in the controller action method, you can access the association entity (Meals-Ingredients) from the Meal entity. From the association entity, you can access the Ingredients entity to get the list of ingredients. The view would look something like this:

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage" %>
    
    
        

    <%= Model.Title %>

    <%= Model.Description %>


    Ingredients:

      <% foreach(Ingredient item in Model.Ingredients) { %>
    • <%= item.Name %>
    • <% } %>

    <%= Html.ActionLink("Update", "Edit", new { id = Model.Meal_ID }) %> | <%= Html.ActionLink("Add Ingredient", "IngredientCreate", new{ id = Model.Meal_ID }) %> | <%= Html.ActionLink("Delete", "Delete", new { id = Model.Meal_ID }) %> | <%= Html.ActionLink("Back to Menu List", "Index") %>

    If your database schema is correctly set up with the foreign key relationships you require, this approach should give you the outcome you're looking for.

提交回复
热议问题