下拉列表看起来不错但不起作用

时间:2018-01-16 15:27:07

标签: c# asp.net-mvc

我正在尝试在asp net MVC中编码,我的代码看起来是:

控制器:

public class ReservationsController : Controller
{
    private AppDbContext db = new AppDbContext();

    // GET: Reservations
    public ActionResult Index()
    {
        return View(db.Reservations.ToList());
    }

    // GET: Reservations/Create
    public ActionResult Create()
    {
        ViewBag.ScreeningId = new SelectList(db.Screenings, "Id", "Description");

        HashSet<int> seats = new HashSet<int>(db.Reservations.Select(x => x.SeatNumber));
        ViewBag.Seats = seats;
        return View();
    }

查看:

<div class="form-group">
    @Html.LabelFor(model => model.Screening, "Seans", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("ScreeningId", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Screening, "", new { @class = "text-danger" })
    </div>
</div>

型号:

public class Reservation
{
    public int Id { get; set; }
    [DisplayName("Imię")]
    public string FirstName { get; set; }
    [DisplayName("Nazwisko")]
    public string SecondName { get; set; }
    [DisplayName("Telefon")]
    public string Phone { get; set; }
    public int? ScreeningId { get; set; }
    [DisplayName("Seans")]
    public Screening Screening { get; set; }
    [DisplayName("Numer miejsca")]
    public int SeatNumber { get; set; }
}

我得到错误:

  

没有类型为&#39; IEnumerable&lt;的ViewData项目。 SelectListItem&gt;&#39;有关键&#39; ScreeningId&#39;。

有人知道可能出现什么问题吗?

Create.cshtml FULL

    @model CinemaTicketReservation.Models.Reservation

@{
    ViewBag.Title = "Rezerwacja filmu";
}

<h2>Zarezerwuj film</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SecondName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SecondName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.SecondName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SeatNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SeatNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.SeatNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Screening, "Seans", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("ScreeningId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Screening, "", new { @class = "text-danger" })
        </div>
    </div>

    <table class="table table-bordered w400">
        @for (var i = 0; i < 5; i++)
        {
            <tr>
                @for (var j = 0; j < 5; j++)
                {
                    var k = i * 5 + j + 1;
                        if (((HashSet<int>) ViewBag.Seats).Contains(k))
                        {
                            <td class="red">@k</td>
                        }
                        else
                        {
                            <td class="green">@k</td>
                        }

                }
            </tr>
        }
     </table>

    <br />

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Rezerwuj" class="btn btn-default" />
        </div>
    </div>


</div>
}

@if (Session["Login"] != null)
{
    <div>
        @Html.ActionLink("Powrót", "Index")
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

ReservationController FULL:

public class ReservationsController : Controller
{
    private AppDbContext db = new AppDbContext();

    // GET: Reservations
    public ActionResult Index()
    {
        return View(db.Reservations.ToList());
    }

    // GET: Reservations/Create
    public ActionResult Create()
    {
        ViewBag.ScreeningId = new SelectList(db.Screenings, "Id", "Description");

        HashSet<int> seats = new HashSet<int>(db.Reservations.Select(x => x.SeatNumber));
        ViewBag.Seats = seats;
        return View();
    }

    // POST: Reservations/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,FirstName,SecondName,Phone,SeatNumber")] Reservation reservation)
    {
        if (ModelState.IsValid)
        {
            // sprawdzamy czy miejsce bylo juz zajete
            if (db.Reservations.Select(x => x.SeatNumber).Contains(reservation.SeatNumber))
            {
                return View(reservation);
            }
            db.Reservations.Add(reservation);
            db.SaveChanges();

            if (Session["Login"] != null)
            {
                return RedirectToAction("Index");
            }
            return RedirectToAction("Success");
        }

        return View(reservation);
    }

    public ActionResult Success()
    {
        return View();
    }

    // GET: Reservations/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Reservation reservation = db.Reservations.Find(id);
        if (reservation == null)
        {
            return HttpNotFound();
        }
        return View(reservation);
    }

    // POST: Reservations/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,FirstName,SecondName,Phone,SeatNumber")] Reservation reservation)
    {
        if (ModelState.IsValid)
        {
            db.Entry(reservation).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(reservation);
    }

    // GET: Reservations/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Reservation reservation = db.Reservations.Find(id);
        if (reservation == null)
        {
            return HttpNotFound();
        }
        return View(reservation);
    }

    // POST: Reservations/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Reservation reservation = db.Reservations.Find(id);
        db.Reservations.Remove(reservation);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

这是你想要的?模特在第一篇文章。

1 个答案:

答案 0 :(得分:0)

在您的控制器中,不要忘记在POST操作中填充ViewBag.ScreeningId

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FirstName,SecondName,Phone,SeatNumber")] Reservation reservation)
{
     ViewBag.ScreeningId = new SelectList(db.Screenings, "Id", "Description");
     [...]