dropdownlist看起来不错,但不起作用

编程入门 行业动态 更新时间:2024-10-19 19:49:28
本文介绍了dropdownlist看起来不错,但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在ASP MVC中进行编码,而我的代码看起来像:

I am trying to code in asp net MVC and my code looks:

控制器:

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; } }

我收到错误消息:

没有类型为'IEnumerable<的ViewData项.具有键"ScreeningId"的SelectListItem>.

There is no ViewData item of type 'IEnumerable< SelectListItem >' that has the key 'ScreeningId'.

有人知道怎么了?

Create.cshtml已满

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已满:

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"); }

这是您想要的吗?模型在第一篇文章中.

This is what you wanted? Model is in first post.

推荐答案

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

In your controller, don't forget to populate ViewBag.ScreeningId in POST action too :

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

更多推荐

dropdownlist看起来不错,但不起作用

本文发布于:2023-11-13 00:39:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但不   dropdownlist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!