Home > database >  Can't get ICollection entities from MVC View
Can't get ICollection entities from MVC View

Time:03-04

So I am making an app for a restaurant that is able to handle Event Table Reservations.

What I want to have is an Event model, and a Table model where I will be able to create tables and use them for each event I create afterwards.

The problem is that after I get the tables in the Event Creation, I am not able to save these tables as copies for the Event(more precisely @event.Tables in Create Post is empty):

Event model:

public class Event
    {
        public Event()
        {
            this.Tables = new HashSet<Table>();
        }

        public int Id { get; set; }
        public string EventName { get; set; }

        public String StartDate { get; set; }
        
        public String StartTime { get; set; }
        public string ImageURL { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Table> Tables { get; set; }

    }

Table model:

 public class Table //Model for Table Conf
    {
        public enum ReservationState {
            [Display(Name = "Available")]
            Available,
            [Display(Name = "Processing")]
            Processing,
            [Display(Name = "Reserved")]
            Reserved
        }
        public int Id { get; set; }
        public int Price { get; set; }
        public string TableIdentifier { get; set; }
        public int NPeople { get; set; }
        public ReservationState ReservationStatus { get; set; }
        public virtual int? EventId { get; set; }

    }

EventsController:

 // GET: Events/Create
        public IActionResult Create()
        {
            List<Table> tables = _context.Table.Where(t => t.EventId == null).AsNoTracking().ToList();
            Event newEvent = new Event();
            foreach(Table table in tables)
            {
                newEvent.Tables.Add(table);
            }
            return View(newEvent);
        }

        // POST: Events/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,EventName,StartDate,StartTime,ImageURL,Description,Tables")] Event @event)
        {
            if (ModelState.IsValid)
            {
                foreach (Table table in @event.Tables)
                {
                    Table cloneTable = new Table();
                    _context.Table.Add(cloneTable);
                    int cloneId = cloneTable.Id;
                    var sourceValues = _context.Entry(table).CurrentValues;
                    _context.Entry(cloneTable).CurrentValues.SetValues(sourceValues);

                    cloneTable.EventId = @event.Id;
                    cloneTable.Id = cloneId;
                    
                }
                _context.Add(@event);
                await _context.SaveChangesAsync();
                
                return RedirectToAction(nameof(Index));
            }
            return View(@event);
        }

Event Create View(the part for saving tables, since everything else is working fine, also I can see the tables in the view correctly:

<div >

                @for (var i = 0; i < Model.Tables.Count; i  )
                {

                    <p>@Model.Tables.ElementAt(i).TableIdentifier - @Model.Tables.ElementAt(i).ReservationStatus  - @Model.Tables.ElementAt(i).Id </p>
                    <input type="hidden" asp-for="@Model.Tables.ElementAt(i).Id" name="@Model.Tables.ElementAt(i).Id"
                           value="@Model.Tables.ElementAt(i).Id" />
                    <input type="hidden" asp-for="@Model.Tables.ElementAt(i).NPeople" name="@Model.Tables.ElementAt(i).NPeople"
                           value="@Model.Tables.ElementAt(i).NPeople" />
                    <input type="hidden" asp-for="@Model.Tables.ElementAt(i).Price" name="@Model.Tables.ElementAt(i).Price"
                           value="@Model.Tables.ElementAt(i).Price" />
                    <input type="hidden" asp-for="@Model.Tables.ElementAt(i).TableIdentifier" name="@Model.Tables.ElementAt(i).TableIdentifier"
                           value="@Model.Tables.ElementAt(i).TableIdentifier" />
                    <input type="hidden" asp-for="@Model.Tables.ElementAt(i).EventId" name="@Model.Tables.ElementAt(i).EventId"
                           value="@Model.Tables.ElementAt(i).EventId" />
                    <select asp-for="@Model.Tables.ElementAt(i).ReservationStatus" name="@Model.Tables.ElementAt(i).ReservationStatus" asp-items="Html.GetEnumSelectList<Table.ReservationState>()">
                    </select>
                }
</div>

CodePudding user response:

Name given to each field is not proper.

Replace this code & Try :

<div >


                @for(var i = 0; i<Model.Tables.Count; i  )
                {

                    <p>@Model.Tables.ElementAt(i).TableIdentifier - @Model.Tables.ElementAt(i).ReservationStatus  - @Model.Tables.ElementAt(i).Id</p>
                    <input type = "hidden" asp-for="@Model.Tables.ElementAt(i).Id" name="Tables[@(i)].Id"
                           value="@Model.Tables.ElementAt(i).Id" />
                    <input type = "hidden" asp-for="@Model.Tables.ElementAt(i).NPeople" name="Tables[@(i)].NPeople"
                           value="@Model.Tables.ElementAt(i).NPeople" />
                    <input type = "hidden" asp-for="@Model.Tables.ElementAt(i).Price" name="Tables[@(i)].Price"
                           value="@Model.Tables.ElementAt(i).Price" />
                    <input type = "hidden" asp-for="@Model.Tables.ElementAt(i).TableIdentifier" name="Tables[@(i)].TableIdentifier"
                           value="@Model.Tables.ElementAt(i).TableIdentifier" />
                    <input type = "hidden" asp-for="@Model.Tables.ElementAt(i).EventId" name="Tables[@(i)].EventId"
                           value="@Model.Tables.ElementAt(i).EventId" />
                    <select asp-for="@Model.Tables.ElementAt(i).ReservationStatus" name="Tables[@(i)].ReservationStatus" asp-items="Html.GetEnumSelectList<Table.ReservationState>()">
                    </select>
                }
</div>
  • Related