0

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; 
4
  • You say "causes a crash" like it means nothing. Crashes give you most of the information you usually need to track down the problem, and they shouldn't be ignored as useless information. So what is the exception? What is the stack trace? Commented Jun 15, 2014 at 2:27
  • I have added more info to my post. Commented Jun 15, 2014 at 3:48
  • Well, there you have it. It tells you exactly what the problem is. db.Spendings is a variable of type IDbSet<Spending> not Spending. 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. Commented Jun 15, 2014 at 3:52
  • Thanks Erik. The following worked OK for me. code Spending spending = new Spending(); spending.SpendDate = DateTime.Now; return View(spending); Commented Jun 15, 2014 at 5:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.