【发布时间】:2021-11-20 19:57:48
【问题描述】:
我的数据库有一个大问题,我无法从网页写入。我使用 Dapper 作为我的 ORM,如果这有什么不同?
db.Insert(guest.EditableGuest);给我无效对象错误 但如果我将其更改为 db.InsertAsync(guest.EditableGuest);错误自行解决但我无法将其添加到数据库中,并且由于某种原因它不会显示在我在数据库中有客人列表的页面上?
我决定重新输入页面以防出现问题,VS有时似乎有点挑剔,有一天我出错了,关闭应用程序,重新打开它就可以了?不幸的是,没有用这个。
GuestListController
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());
}
}
除此之外还有更多用于编辑和删除的代码,并且页面没有抛出任何错误,所以这一切都很好。
这是我的数据库助手类
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; }
}
GuestListViewModel
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 带到他们可以看到其他访客及其回复的页面,但这只是暂时的。这会影响我的代码吗,肯定不会?我的代码中没有任何地方有 GuestLists,所以我不明白它在哪里捡到这个?如果需要,我可以提供更多代码。
提前感谢您的帮助!
【问题讨论】:
-
数据库中有这张表吗?
-
@vivek nuna 是的,我刚刚编辑了原始帖子并包含了数据库图片
-
我认为您应该将 guestId 设置为 count + 1 不等于 count 以获取新记录。
-
不幸的是,这没有任何区别,仍然无法插入记录
-
手动增加计数会给您带来麻烦,当出现多个用户和服务器上的繁重任务时
标签: c# sql sql-server asp.net-mvc sqlexception