Model collection inside a Model

komron

I am a complete newbie in asp.net mvc. And i'm stuck with some problem and don't yet know how to solve it. I googled, found some useful things, but they haven't helped me with solving my problem. So let's begin with what i have now:

public partial class Dish
    {
        public Dish()
        {
            PortionFood = new HashSet<PortionFood>();
        }    
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        [JsonIgnore]    
        public virtual DishCategory DishCategory { get; set; }
        [JsonIgnore]
        public virtual ICollection<PortionFood> PortionFood { get; set; }
    }
}
// Food class in a separate file
public partial class Food
    {
        public Food()
        {
            PortionFood = new HashSet<PortionFood>();
        }
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        public float Proteins { get; set; }
        public float Fat { get; set; }
        public float Carbs { get; set; }
        public float Ccal { get; set; }
        public float? Sugar { get; set; }
        public float? AmountOfWater { get; set; }
        [ForeignKey("FoodCategory")]
        public int IdFoodCategory { get; set; }
        [ForeignKey("FoodConsistencyType")]
        public int IdFoodConsistencyType { get; set; }
        [JsonIgnore]
        public virtual FoodCategory FoodCategory { get; set; }
        [JsonIgnore]
        public virtual ICollection<PortionFood> PortionFood { get; set; }
    }
}
// Portion Food Class in a separate file 
public partial class PortionFood
    {
        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [ForeignKey("Food")]
        public int IdFood { get; set; }
        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [ForeignKey("Dish")]
        public int IdDish { get; set; }

        public float Amount { get; set; }

        public bool IsDeleted { get; set; }

        public virtual Dish Dish { get; set; }

        public virtual Food Food { get; set; }
    }
}

PortionFood just specifies what amount of what Food is inside which Dish. Now i have views for classes Dish and Food, and i am able to add new object of each of these two classes(by filling in the view fields and pressing the button) to DB, i am able to edit an existing Dish && an existing Food. See list of all existing Food && Dish objects.
What i want:
When adding/editing a Dish there must be a possibility to add a PortionFood object releated to current Dish(which we are editing or adding). Just by selecting a FoodId in a dropdown list, setting an amount and clicking the button. The PortionFood object must be inserted to PotionFood collection of related Dish and related Food
How can i implement it?
If there're any uncretainties write in a comment, and i'll try to make it clear.
Thank's for help!

FailedUnitTest

I think I see your problem now, you need to populate from ICollection not to it. In that case I suggest using List<SelectListItem> because you can then use DropDownListFor html helper. For example:

public class ViewModel {
    private readonly List<PortionFood> _foods;

    [Display(Name = "Pick a portion")]
    public int SelectedFoodItem { get; set; }

    public IEnumerable<SelectListItem> FoodPortionItems
    {
        get { return new SelectList(_foods, "Id", "Name"); }
    }
}

public class PortionFood {
    public int Id { get; set; }
    public string Name { get; set; }
}

//In your Razor View:
@Html.LabelFor(m => m.SelectedFoodItem)
@Html.DropDownListFor(m => m.SelectedFoodItem, Model.FoodPortionItems)

That should help you populate the dropdown.

PS: I usually like to use ViewModels instead of Models here, that way you only have what you need in the View

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related