SQL Server Microsoft,Data,SqlClient,SqlException -对象名称“访客列表”无效,

d5vmydt9  于 2023-01-25  发布在  其他
关注(0)|答案(1)|浏览(235)

我的数据库有一个大问题,我不能从网页写入它。我使用Dapper作为我的ORM,如果这有区别?
db.插入(访客.可编辑访客);给我一个无效对象错误

,但是如果我把它改成db.InsertAsync(guest.EditableGuest);错误自行解决,但我无法让它添加到数据库,它不会显示在页面上,我有一个名单的客人在数据库中的一些原因?
我决定再次输入页面以防出现问题,VS有时候似乎有点挑剔,有一天我出现了一个错误,关闭了应用程序,重新打开它,它很好?不幸的是没有工作。
访客列表控制器

using Dapper.Contrib.Extensions;
    using FYP_RSVP_MGMT.Helpers;
    using FYP_RSVP_MGMT.Models;
    using FYP_RSVP_MGMT.ViewModels;
    using Microsoft.AspNetCore.Mvc;
    using System.Linq;

    namespace FYP_RSVP_MGMT.Controllers
    {
        public class GuestListController : Controller
        {
            public IActionResult Index()
            {
                GuestListViewModel guest = new GuestListViewModel();

               return View("Index", guest);
    }

    /* Create or Update a guest RSVP Response */

    public IActionResult CreateUpdate(GuestListViewModel guest)
    {
        if (ModelState.IsValid)
        {
            using (var db = DbHelpers.GetConnection())
            {
                /* If a guestID is null, the number of existing guests will be counted
                 * in order to determine what the next guestID will be and will be added 
                 * asynchronously to the DB in case other actions are on going at the same time */
                 
                if (guest.EditableGuest.GuestID == null)
                {
                    guest.EditableGuest.GuestID = guest.Guests.Count;

                    db.Insert<GuestList>(guest.EditableGuest);
                }

                /* If the guest already exists, we are updating their details */
                else
                {
                    GuestList dbItem = db.Get<GuestList>(guest.EditableGuest.GuestID);

                    TryUpdateModelAsync<GuestList>(dbItem, "EditableGuest");

                    db.Update<GuestList>(dbItem);
                }
            }

            /* When a guest submits their RSVP response, it will bring them to the View Guests page - TEMPORARY MEASURE */
            return RedirectToAction("ViewGuestList", guest);
        }

        else
        {
            return View("ViewGuestList", new GuestList());
        }
    }

除此之外,还有更多的编辑和删除代码,页面没有抛出错误,所以一切正常。
这是我的DB Helper类

public class DbHelpers
{
    public static SqlConnection GetConnection()
    {
        return new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=WeddingDB;");
    }
}

访客列表模型

public class GuestList
{
    [ExplicitKey]
    public int? GuestID { get; set; }

    [Required]
    public string GuestName { get; set; }

    [Required]
    public string GuestType { get; set; }

    [Required]
    public string ContactDetails { get; set; }

    public bool PlusOne { get; set; }

    public string PlusOneName { get; set; }

    [Required]
    public string GuestResponse { get; set; }
}

访客列表视图模型

public class GuestListViewModel
{
    /* Creating a new List of all Guests 
     Editable Guest will hold any instance of an object from the list
            that is being added/edited/deleted etc */
    public List<GuestList> Guests { get; set; }

   
    public GuestList EditableGuest { get; set; }

    /* Selecting all of the existing guests from the DB */
    public GuestListViewModel()
    {
        using (var db = DbHelpers.GetConnection())
        {
            this.EditableGuest = new GuestList();

            this.Guests = db.Query<GuestList>("Select * From GuestList").ToList();
        }
    }

 
}

访客列表视图

@model FYP_RSVP_MGMT.ViewModels.GuestListViewModel

    @{
        ViewData["Title"] = "Guest RSVP";
     }

    @* Form to submit RSVP Response *@

    @using (var form = Html.BeginForm("CreateUpdate", "GuestList", FormMethod.Post))
    {
<div class="container" id="GuestRSVP">

    <br />
    <br />
    <br />

    @* The Bride and Groom name plus Wedding Details will eventually change 
        depending on the log in details *@

    <h3> Welcome to [Bride] and [Groom]'s Wedding</h3>
    <h4>[Church Location]</h4>
    <h4>[Wedding Date and Time]</h4>

    <div class="container" id="RSVPTable" style="align-content:center">

        <table>

            <tr>
                <td>Guest Name: </td>
                <td>@Html.TextBoxFor(m => m.EditableGuest.GuestName)</td>

            </tr>

            <tr>
                <td>Guest Type: </td>
                <td>@Html.DropDownListFor(m => m.EditableGuest.GuestType, new List<SelectListItem> { new SelectListItem { Value = "Guest", Text = "Guest" }, new SelectListItem { Value = "Wedding Party", Text = "Wedding Party" } })</td>
            </tr>

            <tr>
                <td>Email Address: </td>
                <td>@Html.TextBoxFor(m => m.EditableGuest.ContactDetails)</td>
            </tr>

            <tr>
                <td>Plus One: </td>
                <td>@Html.CheckBoxFor(m => m.EditableGuest.PlusOne)</td>
            </tr>

            <tr>
                <td>Plus One Name: </td>
                <td>@Html.TextBoxFor(m => m.EditableGuest.PlusOneName)</td>
            </tr>

            <tr>
                <td>RSVP Response:</td>
                <td>@Html.DropDownListFor(m => m.EditableGuest.GuestResponse, new List<SelectListItem> { new SelectListItem { Value = "Accept", Text = "Accept with Pleasure" }, new SelectListItem { Value = "Decline", Text = "Decline with Regret" } })</td>
            </tr>

        </table>

        <br/>

        <button class="btnSubmitRSVP" type="submit" style="text-align:center"> <a asp-controller="GuestList" asp-action="CreateUpdate"></a> @(Model.EditableGuest.GuestID > 0? "Update": "Submit Response") </button>

    </div>

</div>

}
我发送一些页面的地方可能有点奇怪,比如一个访客的RSVPing不应该被带到他们可以看到其他访客和他们的响应的页面,但这只是暂时的。这会影响到我的代码吗?肯定不会?在我的代码中没有任何地方有访客列表,所以我不知道它是从哪里得到的?如果需要,我可以提供更多的代码。
提前感谢您的帮助!

编辑:这是数据库的图片

wqsoz72f

wqsoz72f1#

Dapper假设了一个复数形式的表名。请在GuestList模型类中使用[Table("GuestList")]属性。

相关问题