I am using MVC5 and EF6.1. I am a beginner in MVC.
I used MVC5 scaffolding to create the controller and views. Everything works fine in index, create, delete etc when changing the Spending data.
However in "public ActionResult Create()" - at the bottom here, "db.Spendings" has no awareness of the "SpendDate" property. ie intellisense has no awareness and when running it causes a crash.
(I followed the following answer to initialize a date Current date and time - Default in MVC razor)
My model is...
namespace Spend.Models.SpendFolder { // Spendings public partial class Spending { public DateTime SpendDate { get; set; } // SpendDate // etc } } (It is a partial class as I am using MetadataType for annotations of the model)
DbContext is...
namespace Spend.Models.SpendFolder { public class SpendDbContext : DbContext, ISpendDbContext { public IDbSet<Category> Categories { get; set; } // Category public IDbSet<Spending> Spendings { get; set; } // Spendings //etc } } The Controller is...
namespace Spend.Controllers { public class SpendingsController : Controller { private SpendDbContext db = new SpendDbContext(); //The Create method public ActionResult Create() { PopulateCategoryDropDownList(); db.Spendings.SpendDate = DateTime.Now; return View(); } //etc } } **** ADDED **** Sorry about no info for "causes a crash". Actually I cannot build the project it at all (I tried setting up the date in all sorts of places previously and being a beginner I got confused). There is a blue squigly line under SpendDate in db.Spendings.SpendDate and the error is...
Error 1 'System.Data.Entity.IDbSet' does not contain a definition for 'SpendDate' and no extension method 'SpendDate' accepting a first argument of type 'System.Data.Entity.IDbSet' could be found (are you missing a using directive or an assembly reference?) C:\Apps\Booking Business\WebSpend\Spend\Controllers\SpendingsController.cs 49 26 Spend
The Usings are...
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 Spend.Models.SpendFolder;
db.Spendingsis a variable of typeIDbSet<Spending>notSpending. So it has methods and variables related to IDbSet. This is a collection of Spending objects. It would be like trying to assign the color of a piece of candy to the bag that the candy is shipped in.code Spending spending = new Spending(); spending.SpendDate = DateTime.Now; return View(spending);