MVC C#将列表保存到数据库

时间:2018-02-08 13:48:54

标签: c# asp.net-mvc

我使用CreateAMeeting方法在我的数据库中保存属性。当我检查数据库时,我可以看到除了列表之外保存的所有属性,我无法在数据库中看到我的列表属性。我一直在谷歌搜索,但我无法找到是否可以看到它们或我是否只是没有正确保存它们。如果我使用断点,我可以看到会议(db.Meetings.Add(会议);)模型有我在Times列表中添加的时间。

当我稍后尝试在ChooseTimes方法中检索我的列表时,它为null。我不知道问题是我保存错误还是其他问题。我做错了什么,是否可以在数据库中查看已保存的列表?

我没有完成过foloachloop,不理会它。

public class MeetingController : BaseController
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;
    public static Meeting NewMeeting;
    public static List<DateTime> TempTimes = new List<DateTime>();

    public ActionResult CreateMeeting()
    {
        return View();
    }

    public ActionResult CreateAMeeting(Meeting meeting)
    {
        var userName = User.Identity.Name;
        var user = db.Users.Where(u => u.UserName == userName).SingleOrDefault();
        var model = new MeetingPeopleViewModel();

        meeting.Creator = user;
        meeting.Invited.Add(user);
        meeting.Times = TempTimes;
        meeting.Start = DateTime.Now;
        meeting.End = meeting.Start.AddMinutes(meeting.Minutes);
        db.Meetings.Add(meeting);
        db.SaveChanges();


        return View("AddToMeeting", model);
    }

    public ActionResult AddTempTime(DateTime Start, Meeting meeting)
    {
        TempTimes.Add(Start);
        meeting.Times = TempTimes;
        return View("CreateMeeting", meeting);
    }

    public ActionResult ChooseTimes()
    {
        var userName = User.Identity.Name;
        var user = db.Users.Where(u => u.UserName == userName).SingleOrDefault();
        Meeting meeting = new Meeting();

        foreach(var item in db.Meetings)
        {
            if(item.Invited.Contains(user))
            {
                meeting = item;
            }
        }
        return View(meeting);
    }

这是我的会议模式。

    public class Meeting
{

    public int Id { get; set; }
    public string Title { get; set; }
    public ApplicationUser Creator { get; set; }
    public ICollection<ApplicationUser> Invited { get; set; }
    public double Minutes { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public ICollection<DateTime> Times { get; set; }
    public bool AllDay { get; set; }

    public ICollection<ApplicationUser> User { get; set; }
    public ApplicationUser UserNotis { get; set; }

}

2 个答案:

答案 0 :(得分:0)

据我所知,您无法将文字列表保存到数据库,您可以通过遍历列表并保存每个唯一记录来将列表中的所有项目保存到数据库中。

public class MeetingController : BaseController
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;
    public static Meeting NewMeeting;
    public static List<DateTime> TempTimes = new List<DateTime>();

    public ActionResult CreateMeeting()
    {
       return View();
    }

public ActionResult CreateAMeeting(Meeting meeting)
{
    var userName = User.Identity.Name;
    var user = db.Users.FirstOrDefault(u => u.UserName == userName);
    var model = new MeetingPeopleViewModel();

    meeting.Creator = user;
    meeting.Invited.Add(user);
    meeting.Times = TempTimes;
    meeting.Start = DateTime.Now;
    meeting.End = meeting.Start.AddMinutes(meeting.Minutes);
    db.Meetings.Add(meeting);
    db.SaveChanges();


    return View("AddToMeeting", model);
}

public ActionResult AddTempTime(DateTime Start, Meeting meeting)
{
    TempTimes.Add(Start);
    meeting.Times = TempTimes;
    return View("CreateMeeting", meeting);
}

public ActionResult ChooseTimes()
{
    var userName = User.Identity.Name;
    var user = db.Users.FirstOrDefault(u => u.UserName == userName);
    Meeting meeting = new Meeting();

    List<Meeting> Meetings = db.Meetings.ToList();

    foreach (var item in Meetings)
    {
        if (item.Invited.Contains(user))
        {
            meeting = item;
        }
    }

    return View(meeting);
}

有更好的方法来做你想做的多件事,但这会解决你的问题。

您必须创建一个从数据库填充的会议列表,以便在您迭代时它是一个Enumerable,而不是一个查询能够,

答案 1 :(得分:0)

我试图将我的整个列表保存在一个字段中,这显然是不可能的。所以我不得不用数据库创建一个新模型来保存我的列表中的evrything。如果其他人在列表中保存项目时遇到问题,这是我的保存方法。

    public void SaveTimes(Meeting meeting)
    {
        foreach (var time in TempStart)
        {
            var mt = new MeetingTimes
            {
                MeetingId = meeting.Id,
                Meeting = meeting,
                Time = time
            };
            db.MeetingTimes.Add(mt);
            db.SaveChanges();
        }
    }