4

I think I almost have this working, but I cant figure out how to finish.

Model:

 public class Location
 {
    public int LocationId { get; set; }
    public string SiteCode { get; set; }
    public int PersonId { get; set; }
    public int IncidentId { get; set; }
  }

View Model

    public List<Location> LocationList { get; set; }

Controller:

    [HttpPost]
    public ActionResult AddRecord(RecordViewModel model)
    {
        if (ModelState.IsValid)
        {
            Location location;
            foreach (var loc in model.LocationList)
            {
                location = new Location
                {
                    PersonId = model.PersonId,
                    SiteCode = loc.SiteCode,
                    IncidentId = loc.IncidentId
                };
            }

            using (var db = new MyEntities())
            {
                db.Order.AddObject(incident);
                db.Location.AddObject(location);
                db.Comment.AddObject(comment);

                db.SaveChanges();
            }

The line db.Location.AddObject(location); is receiving empty. How do I get the location list from the foreach, to the db?

3
  • Is the action decorated with [HttpPost]? Also, what's TrespassEntities supposed to be? Commented Jun 20, 2013 at 17:01
  • Yes it Post. Name changed to MyEntities, it is the EF db connection. Commented Jun 20, 2013 at 17:04
  • You should create a repo project to handle database related stuff. Commented Feb 2, 2016 at 9:51

2 Answers 2

5

You're so close!

// create a new list of your entity Location (may not be in namespace Data)
var locationList = new List<Data.Location>();
foreach (var loc in model.LocationList)
{
    var location = new Data.Location
    {
        PersonId = model.PersonId,
        SiteCode = loc.SiteCode,
        IncidentId = loc.IncidentId
    };
    locationList.Add(location);
}

using (var db = new MyEntities())
{
    db.Order.AddObject(incident);

    foreach (var item in LocationList)
    {
        db.Location.AddObject(location);
    }
    db.Comment.AddObject(comment);

    db.SaveChanges();
}

OR: since you already have the LocationList on your model, use that instead

using (var db = new MyEntities())
{
    db.Order.AddObject(incident);

    foreach (var loc in model.LocationList)
    {
        var location = new Data.Location
        {
            PersonId = model.PersonId,
            SiteCode = loc.SiteCode,
            IncidentId = loc.IncidentId
        };

        db.Location.AddObject(location);
    }
    db.Comment.AddObject(comment);

    db.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply use addrange.

db.YourTable.AddRange(YourList)
db.SaveChanges();

db.Location.AddRange(LocationList)
db.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.