本文介绍了dropdownlist看起来不错,但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.net 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; }
}

我收到错误消息:

有人知道怎么了?

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看起来不错,但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 10:43