Adam
Adam

Reputation: 45

asp-for to table connected relations one-to-many

Hello i have two tables: Restaurants and Reviews

Restaurants.cs

 public class Restaurants
    {
        [Key]
        public int RestaurantsId { get; set; }

        public string Name{ get; set; }

        public virtual ICollection<Reviews> Reviews{ get; set; }

    }

Reviews.cs

public class Reviews
    {
        [Key]
        public int ReviewsId { get; set; }


        public string { get; set; }


        [ForeignKey("Restaurants")]
        public int RestaurantsId { get; set; }

        public virtual Restaurants Restaurants{ get; set; }

    }

In "Restaurant detail view" I displays data from the Restaurants table for one restaurant. I would like to add opinions there regarding a given restaurant.

If i have one to one relation i create something like this

<input type="hidden" asp-for="Reviews.RestaurantsId" value="@Model.IdRestauracji" />

....

and it work. It is possible create something like this, but i have one-to-many relation? Exactly, I mean adding a reference to the table Reviews in asp-for

Upvotes: 0

Views: 47

Answers (1)

Yiyi You
Yiyi You

Reputation: 18169

One input can bind one of the reviews.If you want to bind the reviews of the Model,you can use @foreach:

@foreach (var item in Model.Reviews)
{
    <input type="hidden" asp-for="@item.ReviewsId" value="@item.ReviewsId"  id="@item.RestaurantsId"/>
}

Upvotes: 1

Related Questions