I am writing my first ASP.NET MVC project, and very inexperienced in this subject so please bear with me.
I have to use an existing database for a new application I need to build. I have set this up as a datasource, and used the ADO.NET Entity Data Model to select the tables I need. I have one table that holds categories associated with a parent record, joined by a unique ID. This table doesn't appear in the designer but comes the Associations folder. How do I insert records into this table? Or am I going about this completely wrong?
Here is the code I have so far for Create:
//
// POST: /Home/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "KeyID")] KeyDate dateToCreate, int topic, int level, int subject, int audience)
{
dateToCreate.Created = DateTime.Now;
dateToCreate.CreatedBy = User.Identity.Name;
if (!ModelState.IsValid)
return View();
keyDateRepository.Add(dateToCreate);
keyDateRepository.Save();
return RedirectToAction("Index");
}
Here's the code for the repository:
//
// Insert/Delete Methods
public void Add(KeyDate keyDate)
{
db.KeyDates.AddObject(keyDate);
}
[...]
//
// Persistence
public void Save()
{
db.SaveChanges();
}
Any help much appreciated.
Well, I figured it out. I didn't realise that all I needed to do was to add the categories to the instance of that object.
dateToCreate.Categories.Add(keyDateRepository.GetCategory(topic));
dateToCreate.Categories.Add(keyDateRepository.GetCategory(level));
dateToCreate.Categories.Add(keyDateRepository.GetCategory(subject));
dateToCreate.Categories.Add(keyDateRepository.GetCategory(audience));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.