Can anyone tell me where I am going wrong in this code? I cant figure out the null reference.
All my code is below. I am exhausting myself. This is for a school project.
I have tried several different things, but none have seemed to work. Any suggestions would be appreciated.It worked, but when I rebooted it today it no longer worked.
enter image description here
using Microsoft.AspNetCore.Mvc;
using WeddingInvites.Data;
using WeddingInvites.Models;
namespace WeddingInvites.Controllers
{
public class ResponseController : Controller
{
private GuestContext data { get; set; }
public ResponseController(GuestContext ctx) => data = ctx;
[HttpGet]
public ViewResult ResponseForm()
{
return View();
}
[HttpPost]
public IActionResult ResponseForm(Guest guest)
{
Guest check = data.Guests.FirstOrDefault(g => g.Name == guest.Name)!;
if (check != null)
{
ModelState.AddModelError("Name", $"{guest.Name} has already submitted their RSVP.");
}
if (ModelState.IsValid)
{
data.Guests.Add(guest);
data.SaveChanges();
return View("SubmissionThanks");
}
else
{
ModelState.AddModelError("", "Please correct the errors.");
return View();
}
}
public ViewResult ListResponses()
{
return View();
}
}
}
@model Guest
@{
ViewData["Title"] = "Wedding RSVP";
}
<div class="text-center">
<h2>Thanks for letting us know, @Model.Name</h2>
<h4> @if(Model.WillAttend == true)
{
@:We are excited that you are able to celebrate our big day with us! Let the countdown for Mr. and Mrs. begin!
}
else
{
@:Ah shucks, we will miss you! It stinks that you can't attend, but thanks for your best wishes and for letting us know!
}
</h4>
</div>
<div class="text-center">Click <a asp-action="ListResponses" class="btn btn-sm btn-dark">here</a> to see the RSVP responses or close the browser if you are done.</div>
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System.ComponentModel.DataAnnotations;
namespace WeddingInvites.Models
{
public class Guest
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter your name.")]
public string? Name { get; set; }
[Required(ErrorMessage = "Please enter your email address.")]
[RegularExpression(".+\@.+\..+", ErrorMessage = "Please enter a valid email address.")]
public string? Email { get; set; }
[Required(ErrorMessage = "Please enter your phone number.")]
public string? Phone { get; set; }
[Required(ErrorMessage = "Please specify whether you'll be in attendance or not.")]
public bool? WillAttend { get; set; }
}
}
@model Guest
@{
string title = "Wedding RSVP Submission Form";
ViewData["Title"] = title;
}
@section scripts {
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
<h3>Please complete the form below:</h3>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<form asp-action="ResponseForm" method="post">
<div class="mb-3">
<label asp-for="Name" class="form-label">Your Name:</label>
<span asp-validation-for="Name" class="text-danger"></span>
<input asp-for="Name" class="form-control"/>
</div>
<div class="mb-3">
<label asp-for="Email" class="form-label">Your Email:</label>
<span asp-validation-for="Email" class="text-danger"></span>
<input asp-for="Email" class="form-control" />
</div>
<div class="mb-3">
<label asp-for="Phone" class="form-label">Your Phone Number:</label>
<span asp-validation-for="Phone" class="text-danger"></span>
<input asp-for="Phone" class="form-control" />
</div>
<div class="mb-3">
<label asp-for="WillAttend" class="form-label">Will you be in attendance?</label>
<span asp-validation-for="WillAttend" class="text-danger"></span>
<select asp-for="WillAttend" class="form-select">
<option value="">Choose an option</option>
<option value="true">Yes, Count me in!</option>
<option value="false">I can't make it, but I send my sincerest congratulations to y'all!</option>
</select>
</div>
<button type="submit" class="btn btn-dark">Submit</button>
<a asp-controller="Home" asp-action="Index" class="btn btn-dark">Back to Greeting</a>
</form>
@model List<Guest>
@{
ViewData["Title"] = "Wedding RSVP";
}
<h2 class="text-center text-warning">The list of Attendees</h2>
<table class="table table-dark table-hover">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
</tr>
</thead>
@foreach (Guest guest in Model)
{
<tr>
<td>@guest.Name</td>
<td>@guest.Email</td>
<td>@guest.Phone</td>
</tr>
}
</table>
<a asp-controller="Home" asp-action="Index" class="btn btn-dark">Back to Greetings</a>
using Microsoft.EntityFrameworkCore;
using WeddingInvites.Models;
namespace WeddingInvites.Data
{
public class GuestContext:DbContext
{
public GuestContext(DbContextOptions<GuestContext> options) : base(options)
{ }
public DbSet<Guest> Guests { get; set; } = null!;
}
}
New contributor
kalebt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.