I have the following View (cshtml):
@model Kino.Entities.Models.Reservation
@{
ViewBag.Title = "Create";
KinoStudenckieNiebieskiKlocek.Entities.Models.Seans s = ViewBag.Seans;
Layout = "~/Views/Shared/_Layout.cshtml";
int size = 50;
int margin = 5;
int seatsInRows = s.CinemaHall.Seats.Max(x => x.NumberInRow);
}
@*@{
Model.SeansId = s.Id;
Model.Payed = false;
}*@
<script>
var list = [];
</script>
<script>
function onClick(object) {
console.log(object);
var x = document.getElementById(object);
if (x.style.backgroundColor == "green") {
x.style.backgroundColor = "orange";
list.push(object);
}
else {
x.style.backgroundColor = "green";
list.splice((list.indexOf(object)), 1);
}
console.log(list)
}
</script>
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<form class="form-horizontal">
<h4>Reservation</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div>
<p>
Film: @s.Movie.MovieName
</p>
<p>
Numer sali @s.Id
</p>
<div style="width:@((size+margin)*(seatsInRows+1))px">
@foreach (var seat in s.CinemaHall.Seats)
{
bool zarezerwowane = false;
foreach (var reservation in @seat.Reservations)
{
if (reservation.Seans.Id == s.Id)
{
zarezerwowane = true;
}
}
<button name="@seat.Id" id="@seat.Id" type="button" style="background-color:@(zarezerwowane ? "red" : "green");width:@(size)px;height:@(size)px;"
onclick="@(!zarezerwowane ? "onClick("[email protected]+")" : "")">
@seat.Row,@seat.NumberInRow
</button>
}
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.Payed, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Payed)
@Html.ValidationMessageFor(model => model.Payed, "", new { @class = "text-danger" })
</div>
</div>
</div>*@
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@*<input type="submit" value="Create" class="btn btn-default" />*@
<button type="submit" class="btn btn-default">Zarezerwuj</button>
</div>
</div>
</form>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
and Controller (C#) as destination of my post message:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Kino.Entities;
using Kino.Entities.Models;
using Kino.DAL.Repositories.Abstract;
namespace Kino.Controllers
{
public class ReservationsController : Controller
{
private readonly IReservationRepository _reservationRepository;
private readonly ISeansRepository _seansRepository;
public ReservationsController(IReservationRepository reservationRepository, ISeansRepository seansRepository)
{
_reservationRepository = reservationRepository;
_seansRepository = seansRepository;
}
// GET: Reservations
public async Task<ActionResult> Index()
{
var reservations = await _reservationRepository.GetReservationsAsync();
return View(reservations);
}
// GET: Reservations/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Reservation reservation = await _reservationRepository.GetReservationAsync(id.Value);
if (reservation == null)
{
return HttpNotFound();
}
return View(reservation);
}
// GET: Reservations/Create
public async Task<ActionResult> Create(int id)
{
var result = await _seansRepository.GetSeansAsync(id);
ViewBag.Seans = result;
return View();
}
// POST: Reservations/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Email,ReservedSeats")] Reservation reservation)
{
if (ModelState.IsValid)
{
reservation.Payed = false;
await _reservationRepository.SaveReservationAsync(reservation);
return RedirectToAction("Index");
}
return View(reservation);
}
// GET: Reservations/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Reservation reservation = await _reservationRepository.GetReservationAsync(id.Value);
if (reservation == null)
{
return HttpNotFound();
}
return View(reservation);
}
// POST: Reservations/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,Email,Payed")] Reservation reservation)
{
if (ModelState.IsValid)
{
await _reservationRepository.SaveReservationAsync(reservation);
return RedirectToAction("Index");
}
return View(reservation);
}
// GET: Reservations/Delete/5
public async Task<ActionResult> Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Reservation reservation = await _reservationRepository.GetReservationAsync(id.Value);
if (reservation == null)
{
return HttpNotFound();
}
return View(reservation);
}
// POST: Reservations/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
Reservation reservation = await _reservationRepository.GetReservationAsync(id);
await _reservationRepository.DeleteReservationAsync(reservation);
return RedirectToAction("Index");
}
}
}
What I want is to send collection of seats to my controller and then save it to my database.
Once saved as reserved they cannot be send again.
How could I hook buttons to my form so I will be able to receive that collection in my controller?
question from:
https://stackoverflow.com/questions/65906781/send-a-collection-from-view-to-the-mvc-controller 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…