EDIT: I have the Quote working on EDIT, but it's not working on Create. I've adjusted my method from my original post. So my issue is I cannot figure out to pass the ID of a newly created record to the quote method.
Student here! Creating an MVC app and I cannot figure out I'm missing. I thought I had it working and then I moved on, but when I went back to test, I realized it wasn't working and am getting a bunch of errors. I know the reason is because the values I think I'm passing aren't actually being passed.
I have the two scenarios, Create and Edit and when the DB entry is created it should redirect to my quote method, but that is where I'm getting errors. I can't figure out how to pass the values that were just created to the quote method. Any suggestions as to what I'm missing would be appreciated!
This is my controller:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CarInsurance.Models;
using CarInsurance.View_Models;
namespace CarInsurance.Controllers
{
public class InsureeController : Controller
{
private InsuranceEntities db = new InsuranceEntities();
// GET: Insuree/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Insuree insuree = db.Insurees.Find(id);
if (insuree == null)
{
return HttpNotFound();
}
return View(insuree);
}
// GET: Insuree/Create
public ActionResult Create()
{
return View();
}
// POST: Insuree/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "FirstName,LastName,EmailAddress,DateOfBirth,CarYear,CarMake,CarModel,DUI,SpeedingTickets,CoverageType")] Insuree insuree)
{
if (ModelState.IsValid)
{
db.Insurees.Add(insuree);
db.SaveChanges();
}
return RedirectToAction("Quote", insuree);
}
// GET: Insuree/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Insuree insuree = db.Insurees.Find(id);
if (insuree == null)
{
return HttpNotFound();
}
return View(insuree);
}
// POST: Insuree/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,EmailAddress,DateOfBirth,CarYear,CarMake,CarModel,DUI,SpeedingTickets,CoverageType")] Insuree insuree)
{
if (ModelState.IsValid)
{
db.Entry(insuree).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Quote", insuree);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
// GET: Quote
public ActionResult Quote(int? id)
{
Insuree insuree = db.Insurees.Find(id);
Quote quote = new Quote();
quote.FirstName = insuree.FirstName;
quote.LastName = insuree.LastName;
quote.EmailAddress = insuree.EmailAddress;
quote.DateOfBirth = insuree.DateOfBirth;
quote.CarYear = insuree.CarYear;
quote.CarMake = insuree.CarMake;
quote.CarModel = insuree.CarModel.ToLower();
quote.DUI = Convert.ToBoolean(insuree.DUI);
quote.SpeedingTickets = insuree.SpeedingTickets;
quote.CoverageType = Convert.ToBoolean(insuree.CoverageType);
var age = DateTime.Now.Year - insuree.DateOfBirth.Year;
decimal totalQuote = 50;
// Age Constraints
if (age <= 18)
{
totalQuote += 100;
quote.quoteAge = 100;
}
if (age >= 19 && age <= 25)
{
totalQuote += 50;
quote.quoteAge = 50;
}
if (age > 25)
{
totalQuote += 25;
quote.quoteAge = 25;
}
// Car Constraints
if (quote.CarYear < 2000 || quote.CarYear > 2015)
{
totalQuote += 25;
quote.quoteCar = 25;
}
if (quote.CarMake == "porsche")
{
totalQuote += 25;
quote.quoteCar += 25;
}
if (quote.CarModel == "911 carrera")
{
totalQuote += 25;
quote.quoteCar += 25;
}
// Driving History Constraints
if (quote.SpeedingTickets > 0)
{
totalQuote += quote.SpeedingTickets * 10;
quote.quoteTickets = quote.SpeedingTickets * 10;
}
if (quote.DUI)
{
totalQuote *= Convert.ToDecimal(1.25);
quote.quoteDUI = totalQuote * Convert.ToDecimal(.25);
}
// Coverage Requested
if (quote.CoverageType)
{
totalQuote *= Convert.ToDecimal(1.50);
}
ViewBag.Quote = Math.Round(totalQuote, 2);
return View(quote);
}
}
}
And my quote class if needed:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CarInsurance.View_Models
{
public class Quote
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public DateTime DateOfBirth { get; set; }
public int CarYear { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
public bool DUI { get; set; }
public int SpeedingTickets { get; set; }
public bool CoverageType { get; set; }
public decimal quoteAge { get; set; }
public decimal quoteCar { get; set; }
public decimal quoteTickets { get; set; }
public decimal quoteDUI { get; set; }
}
}