I'm trying to insert a customer in my database from the create page and I keep getting a null reference. My model is structured like like so:
Model
public string Name { get; set; }
...
public string FirstName { get { return Name.Substring(0, Name.IndexOf(" ")); } }
public string LastName { get { return Name.Substring(Name.IndexOf(" ") + 1); } }
My controller is setup like so:
[HttpPost]
public ActionResult Create(Customer customer)
{
if (ModelState.IsValid)
{
Customer add = new Customer();
add.Name = customer.FirstName + " " + customer.LastName;
db.Customers.Add(add);
db.SaveChanges();
I understand that when inserting data, the Name field will return a null because it doesn't have anything in the beginning. How should should I adjust this?