HttpPost не сохраняет данные в репозитории после RedirectToAction - PullRequest
0 голосов
/ 06 октября 2019

Я пытаюсь внедрить простую систему регистрации гостей. Я хочу перенаправить в свой список гостей после регистрации. Однако данные регистрации не сохраняются. В настоящее время я использую «жестко запрограммированную» базу данных, так как я новичок в MVC. Моя база данных хранится в классе репозитория.

GuestRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TablePlanningApp.Models;

namespace TablePlanningApp.Data
{
    public class GuestsRepository
    {
        public List<Guest> _guests { get; set; }

        public GuestsRepository()
        {
            _guests = new List<Guest>();

        }

        public List<Guest> GetGuests()
        {
            return _guests;
        }

        public void AddGuest(Guest guest)
        {
            _guests.Add(guest);
        }
    }
}

GuestController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TablePlanningApp.Models;
using TablePlanningApp.Data;

namespace TablePlanningApp.Controllers
{
    public class GuestsController : Controller
    {

        private readonly GuestsRepository _guestsRepository = null;

        public GuestsController()
        {
            _guestsRepository = new GuestsRepository();
        }
        public ActionResult Index()
        {
            var guests = _guestsRepository.GetGuests();

            return View(guests);
        }

        [HttpGet]
        public ActionResult AddGuest()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddGuest(Guest guest)
        {
            _guestsRepository.AddGuest(guest);
            return RedirectToAction("Index");
        }
    }
}

AddGuest.cshtml

@model TablePlanningApp.Models.Guest
@{
    ViewBag.Title = "Register Guest";
}

<h2>Register Guest</h2>

@using (Html.BeginForm())
{

    <div class="form-group">
        @Html.LabelFor(m => m.Name, "Your Name")
        @Html.TextBoxFor(m => m.Name, null, new { @class = "form-control", placeholder = "Enter Guest Name" })
        <small id="note" class="form-text text-muted">RSVP for accompanying guests below. Do not need to enter accompanying guests' name.</small>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Contact, "Your Contact Number")
        @Html.TextBoxFor(m => m.Contact, null, new { @class = "form-control", placeholder = "Your contact" })

    </div>

    <div class="form-group">
        @Html.DisplayNameFor(m => m.IsVegetarian) @Html.RadioButtonFor(m => m.IsVegetarian, true)
    </div>
    <div class="form-group">
        @Html.DisplayNameFor(m => m.IsHalal) @Html.RadioButtonFor(m => m.IsHalal, true)
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
}

Я ожидал, что любой гость будет заполнен после того, как я нажму кнопку отправки, но список все еще был пустым после отправки. Спасибо за любую помощь, спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...