Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:07:06+00:00 2026-06-05T02:07:06+00:00

I have three related tables. Calendar 1…* CalendarUser *…1 User. When I have edited

  • 0

I have three related tables. Calendar 1…* CalendarUser *…1 User. When I have edited the CalendarUsers in the edit calendar view I then post the ViewModel back to the controller. Here is my controller code:

    [HttpPost]
    public ActionResult Edit(int id, CreateCalendarViewModel cvm)
    {
        long userId = long.Parse(User.Identity.Name);

        db.Calendars.Attach(cvm.CurrentCalendar);
        cvm.Users= DbExtensions.GetUserList(userId);

        if (ModelState.IsValid)
        {
            ////Remove the deselected users
            cvm.CurrentCalendar.CalendarUsers.Clear();

            //Get the names from the selected users
            var selectedUsers = from u in cvm.Users
                                where cvm.SelectedUsers.Contains(u.Key)
                                select new KeyValuePair<long, string>(long.Parse(u.Key), u.Value);

            foreach (var selectedUser in selectedUsers)
            {

                User user = db.Users.Find(selectedUser.Key);

                //If usr does not exist create a new
                if (user == null)
                {
                    db.Users.Add(new User
                    {
                        UserId = selectedUser.Key,
                        Name = selectedUser.Value,
                        Expires = DateTime.Now,
                        AccessToken = string.Empty
                    });
                }

                //Add the binding to the calendar
                cvm.CurrentCalendar.CalendarUsers.Add(new CalendarUser
                {
                    CalendarId = cvm.CurrentCalendar.CalendarId,
                    UserId = selectedUser.Key
                });
            }

            db.Entry(cvm.CurrentCalendar).State = EntityState.Modified;

            db.SaveChanges();

        }

        return View(cvm);
    }

Here are my classes:

public partial class Calendar 
{ 
    public Calendar() 
    { 
        this.CalendarUsers = new HashSet<CalendarUser>(); 
    } 

    public int CalendarId { get; set; } 
    public string CalendarTitle { get; set; } 
    public string CalendarDescription { get; set; } 
    public long UserId { get; set; } 

    public virtual User User { get; set; } 
    public virtual ICollection<CalendarUser> CalendarUsers { get; set; } 
} 

public partial class CalendarUser 
{ 
    public int CalendarUserId { get; set; } 
    public int CalendarId { get; set; } 
    public long UserId { get; set; } 
    public Nullable<bool> IsAdmin { get; set; } 

    public virtual Calendar Calendar { get; set; } 
    public virtual User User { get; set; } 
} 

public partial class User 
{ 
   public User() 
   { 
       this.Calendars = new HashSet<Calendar>(); 
       this.CalendarUsers = new HashSet<CalendarUser>(); 
   } 

   public long UserId { get; set; } 
   public string Name { get; set; } 

   public virtual ICollection<Calendar> Calendars { get; set; } 
   public virtual ICollection<CalendarUser> CalendarUsers { get; set; } 
} 

For some reason when i save the changes the calendar is being deleted as well? I’ve searched a bit but noone seem to have the same problem? Am I doing it wrong? Is there a better way to update/remove related entities?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:07:08+00:00Added an answer on June 5, 2026 at 2:07 am

    It seems that I forgot to include a hidden field in in the view containing the id of the user and the result was that when I updated the calendar it saved with Id = 0 and thus hid the objects in the view for the specified user. Mental note: Always verify in the database what is really happening.

    I also need to look into whats happening when I send objects back and forth between views and controller. Sometimes it seems to manage by itself and sometimes I need to specify all the fields myself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables related to each other. They represent a hierarchical object. A_Table->
I have a three database tables related for example: company( one - one) Contact(
i am using JSP. Now assume I have three tables in mysql related as
I have a database with three tables, related with foreign keys: Trips: | ID|
I have three MySQL Tables: customers, persons, and dates_d customers and persons are related
I have three tables in MySQL that are related, yet not technically linked to
i have three tables in my database book,author,publishing ... i want the user can
If I have three tables, A/B/C, A is related to B and B is
In my app I have three tables: Users Profiles Friends The User has one
I have lots of tables with statistical data (environment related) and wondering if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.