在View中创建列表以添加到Model的ICollection属性

时间:2014-12-07 15:20:45

标签: asp.net-mvc entity-framework-6

我正在尝试创建一个允许用户上传食谱的网站

所以我有一个名为Recipe的模型类,它看起来像是

[Table("Recipe")]
public class Recipe
{
    public int ImageMimeType { get; set; }
    public byte[] Thumbnail { get; set; }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int RecipeID { get; set; }
    public string BakeryName { get; set; }
    public string Description { get; set; }
    public byte[] ImageData { get; set; }
    public int EndorsementCount { get; set; }
    public DateTime LastModified { get; set; }

    [Required]
    public int UserId { get; set; }

    public virtual ICollection<Ingredient> Ingredients { get; set; }
    public virtual ICollection<Method> Methods { get; set; }


}

我的成分类看起来如下:

[Table("Ingredient")]
public class Ingredient
{
    [Key]
    public int IngredientID { get; set; }

    [Required]
    public int RecipeID { get; set; }

    public string ItemName { get; set; }
    public int Measurement { get; set; }
    public string MeasurementUnit { get; set; }
}

在我的视图中,我想动态添加成分:用户点击“添加&#39;按钮,在她想要的时候添加另一种成分。

我的问题是: 我应该如何在视图中为成分创建一个列表,并在提交表单时将其传递给Controller的创建方法?

问题是,由于RecipeID是自动生成的,我在查看时不知道我的RecipeID将会是什么,直到我将我的食谱保存在数据库表中。但是如何将这个RecipeID传递给我的每种成分?

非常感谢任何帮助!

0 个答案:

没有答案