如何链接到数据库中的项目而不是再次创建相同的项目

时间:2014-10-21 16:49:31

标签: c# linq asp.net-mvc-4 razor local-database

我只是C#,MVC4,razor和Linq的初级程序员。现在我正忙着为不存在的酒店建立一个应用程序。该应用程序的工作方式几乎与其相似。但是有一个小错误。当我创建预订时,我想链接一个房间和一个列表<>客人预订。但每当我创建一个新的预订时,在数据库中创建了一个新的房间,除了id之外,一切都是相同的。对于客人而言,如果我创建新的预订,客人也会在数据库中创建并且不与预订相关联。我使用了调试器和对象Room,列表完美地传递给了预订的创建。所以在我的预订创建方法中一定出错了。我无法找出我的Create方法有什么问题。所以我希望能在这里找到一些帮助。任何帮助将不胜感激。

其他信息: TempStore是一个静态类,我可以暂时保存一些变量。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Booking booking)
    {
        if (ModelState.IsValid)
        {
            if (TempStore.tempGuests.Count == 0 || TempStore.tempRoom == null)
            {
                ModelState.AddModelError("NoGuestsSelected", "Please select the persons for your room.");
                ModelState.AddModelError("NoRoomSelected", "Please select a room for your booking.");
                return View();
            }
            else
            {
                booking.Room = TempStore.tempRoom;
                booking.Guests = TempStore.tempGuests;
                booking.PriceTotal = TempStore.priceTotal;
                booking.AveragePrice = TempStore.averagePrice;
                booking.StartDate = TempStore.startDate;
                booking.EndDate = TempStore.endDate;

                db.Bookings.Add(booking);
                db.SaveChanges();

                //Clearing the TempStore cause everything has been added
                TempStore.tempGuests.Clear();
                TempStore.tempRoom = null;

                return RedirectToAction("Index");
            }
        }

        return View(booking);
    }

这是我的TempStore

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hotel.Models
{
    public static class TempStore
    {
        public static Room tempRoom;
        public static ICollection<Guest> tempGuests = new List<Guest>();
        public static DateTime startDate;
        public static DateTime endDate;
        public static decimal priceTotal;
        public static decimal averagePrice;
    }
}

这是我设置我的客人的地方

    public ActionResult AddGuest(int id)
    {
        Guest g = db.Guests.Find(id);

        if (TempStore.tempGuests.Count != 0)
        {
            foreach (Guest gue in TempStore.tempGuests)
            {
                if (gue == g)
                {
                    ModelState.AddModelError("DuplicateGuest", "Cannot select the same guest twice.");
                    return RedirectToAction("SelectGuest");
                }
            }
        }

        TempStore.tempGuests.Add(g);

        return RedirectToAction("SelectGuest");
    }

这是我为预订设置房间的地方:

    public ActionResult AddRoom(int id)
    {
        Room r = db.Rooms.Find(id);
        TempStore.tempRoom = r;
        return RedirectToAction("SelectGuest");
    }

2 个答案:

答案 0 :(得分:0)

嗯,基本上你必须有办法确定它是重复的。这取决于您的数据模型,但如果您知道的字段是唯一的,则可以在保存新记录之前搜索匹配的记录。

答案 1 :(得分:0)

它可能不是一个整洁的方式。但我的问题解决了。我要感谢大家的帮助。

这就是我解决它的方法:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Booking booking)
    {
        if (ModelState.IsValid)
        {
            if (TempStore.tempGuests.Count == 0 || TempStore.tempRoom == null)
            {
                ModelState.AddModelError("NoGuestsSelected", "Please select the persons for your room.");
                ModelState.AddModelError("NoRoomSelected", "Please select a room for your booking.");
                return View();
            }
            else
            {
                booking.PriceTotal = TempStore.priceTotal;
                booking.AveragePrice = TempStore.averagePrice;
                booking.StartDate = TempStore.startDate;
                booking.EndDate = TempStore.endDate;

                db.Bookings.Add(booking);
                db.SaveChanges();

                booking.Guests = new List<Guest>();

                foreach(Guest g in TempStore.tempGuests)
                {
                    db.Bookings.Find(booking.Id).Guests.Add(db.Guests.Find(g.Id));
                }

                db.Bookings.Find(booking.Id).Room = db.Rooms.Find(TempStore.tempRoom.Id);
                db.Entry(booking).State = EntityState.Modified;
                db.SaveChanges();

                //Clearing the TempStore cause everything has been added
                TempStore.tempGuests.Clear();
                TempStore.tempRoom = null;

                return RedirectToAction("Index");
            }
        }

        return View(booking);
    }