将集合从 View 发送到 MVC 控制器

问题描述

我有以下视图(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("+@seat.Id+")" : "")">
                        @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>

和控制器 (C#) 作为我的帖子消息的目的地:

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,for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Id,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");
        }
    }
}

我想要的是将座位集合发送到我的控制器,然后将其保存到我的数据库中。 一旦保存为保留,它们就不能再次发送。 如何将按钮挂钩到我的表单,以便我能够在我的控制器中接收该集合?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)