0

我有三个相关的表。日历 1...* CalendarUser *...1 用户。当我在编辑日历视图中编辑了 CalendarUsers 后,我将 ViewModel 发布回控制器。这是我的控制器代码:

    [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);
    }

这是我的课程:

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; } 
} 

出于某种原因,当我保存更改时,日历也被删除了?我搜索了一下,但似乎没有人有同样的问题?我做错了吗?有没有更好的方法来更新/删除相关实体?

4

1 回答 1

1

似乎我忘记在包含用户 id 的视图中包含一个隐藏字段,结果是当我更新日历时,它使用 Id = 0 保存,因此在视图中为指定用户隐藏了对象。心理提示:始终在数据库中验证实际发生的情况。

当我在视图和控制器之间来回发送对象时,我还需要研究发生了什么。有时它似乎可以自行管理,有时我需要自己指定所有字段。

于 2012-06-01T11:06:01.267 回答