Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net:如何将两个模型组合在一起以创建父实体及其所有子实体?_C#_Asp.net_Asp.net Mvc_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# asp.net:如何将两个模型组合在一起以创建父实体及其所有子实体?

C# asp.net:如何将两个模型组合在一起以创建父实体及其所有子实体?,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我不熟悉使用ASP.net和MVC,需要一些帮助。我目前正在从事一个项目,该项目是一本在线食谱书,我有各种表格,但我想合并成一个视图的两个表格是recipe和RecipeInElements,如下所示: Recipe.cs namespace TheOnlineFoodBook.Models { public class Recipe { [DatabaseGeneratedAttribute(DatabaseGenerat

我不熟悉使用ASP.net和MVC,需要一些帮助。我目前正在从事一个项目,该项目是一本在线食谱书,我有各种表格,但我想合并成一个视图的两个表格是recipe和RecipeInElements,如下所示:

Recipe.cs

    namespace TheOnlineFoodBook.Models
    {
        public class Recipe
        {
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int RecipeID { get; set; }
            [Display(Name = "Recipe")]
            public string RecipeName { get; set; }
            [Display(Name= "Cuisine")]
            public int CuisineID { get; set; }
            public string Directions { get; set; }
            [Display(Name = "Preperation Time:")]
            public double PreperationTime { get; set; }
            [Display(Name = "Cooking Time:")]
            public double CookingTime { get; set; }


            public virtual List<RecipeIngredient> RecipeIngredients { get; set; }

            [ForeignKey("CuisineID")]
            public virtual Cuisine Cuisine { get; set; }
        }
    }
    public class RecipeIngredient
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int RecipeID { get; set; }
        public int IngredientID { get; set; }
        public double Quantity { get; set; }
        public int MeasurementID { get; set; }
        public string Notes { get; set; }

        public virtual Recipe Recipe { get; set; }
        public virtual Measurement Measurement { get; set; }
        public virtual Ingredient Ingredient { get; set; }
    }
}

我已经读了一些关于组合模型来创建viewModel的内容,但我不确定如何进行,我还希望能够在创建配方时同时添加多个RecipeIndents条目。目前,我有基本的CRUD视图,用于为我创建的实体框架。

这能满足您的需要吗

public class RecipeViewModel
{
    public Recipe Recipe { get; set; }
    public List<RecipeIngredients> RecipeIngredientsList { get; set; }
}
公共类RecipeViewModel
{
公共配方{get;set;}
公共列表RecipeIngredientsList{get;set;}
}

您将无法有效地使用数据注释,但如果这不是一个问题,您现在将能够将多个
RecipeCredit
附加到页面的同一
配方中。

您在“RecipeCredit”中已经有一个“Recipe”对象,因此在视图中使用“RecipeCredit”作为模型将允许您访问“Recipe”属性,您可以采取的另一种方法是在传递
新元组(new Recipe(),new RecipeIngredient());
之后在视图中使用
@modelTuple
,就像传递模型一样,但我看不到后面的方法的用处,因为“RecipeIngredient”已经有“Recipe”“

我已经创建了一个这样的视图模型,我只是不确定从那里去哪里。您可以像加载任何其他类一样加载视图模型,然后将该模型传递给视图<代码>返回视图(模型)在视图中,您可以像访问任何其他类一样访问每个属性<代码>模型.配方.RecipeName
模型.RecipeIngredientsList[i].注释