如何防止用户在asp.net中的sql数据中预订现有约会?

时间:2015-03-18 15:55:43

标签: c# sql asp.net visual-studio

这是我的约会控制器,我在那里创建了约会代码,我可以在这里添加什么,所以如果用户与另一个用户同时预约,则不允许他们预约

public class AppointmentController : Controller
{
    Context db = new Context();

    // GET: Appointment
    public ActionResult Index()
    {
        string UserName = User.Identity.Name;
        return View(db.Appointment.ToList().Where(a=> a.username == UserName));
    }


    public ActionResult Create()
    {


        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Appointment appointment)
    {
        try{
            string UserName = User.Identity.Name;
            appointment.username = UserName;
            // TODO: Add insert logic here
           // if (ModelState.IsValid)
           // {
                db.Appointment.Add(appointment);
                db.SaveChanges();

                return RedirectToAction("Index", "Appointment");
           // }

        }
        catch (Exception ex)
        {
            return View(ex.ToString());
        }

    }

2 个答案:

答案 0 :(得分:1)

这似乎是一个ASP MVC控制器。一旦掌握了MVC,MVC通常会更快开发,但学习曲线很陡峭。

有许多可用的MVC教程,例如MVC 5 Tutorial

对于您的原始问题,以下内容可能有效:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Appointment appointment)
{
    //This action will receive a "Model" (the appointment object) from the View (WebPage).
    //Most "HttpPost" methods are attempting to Save data in some way: Update, Create, etc. 
    //The general flow in an MVC Post method is
    //1. Check that the Model is Valid (required fields have a value, numbers don't have letters, etc.)
    //2. Then perform any server side checks (like conflicting schedules)
    //3. Then attempt to save.
    //4. If successful, return to Index. Else, add the Error to the Model and return the Model.

    try{

        //Use LINQ to check if the Appointment the User wants to book
        // 1. Starts before any other appointment ends 
        // 2. AND ALSO Ends after any other appointment starts.
        // If both conditions are true, there is some overlap
        // Making a guess as to what the fields representing Start and Stop times are called.
        // Replace the field names as appropriate
        if (db.Appointment.Where(a => a.EndTime > appointment.StartTime && a.StartTime < appointment.EndTime).Any()) 
            //If any existing appointment exists, then add a model state error.
            ModelState.AddModelError("StartTime", "Time not available.");

        //Using the Logged In User's UserName as the appointment's UserName
        appointment.username = User.Identity.Name;

        //TODO: Add in any other checks or changes here.
        //Example: Check if the appointment is within business hours.
        //They will follow the same format:
        //   Check the Condition
        //   If something is wrong, add the ModelError to the ModelState.

        //If a field is invalid, or we added a ModelError, the ModelState will NOT be valid.
        if (ModelState.IsValid)
        {
            //Add a row to the appointment table.
            db.Appointment.Add(appointment);

            //Save the change.
            db.SaveChanges();

            //Send the user back to Index.
            return RedirectToAction("Index", "Appointment");
        }

    }
    catch (Exception ex)
    {
        //Returns a detailed error message to the User.
        //While helpful for development, this is generally a no-no in production.
        return View(ex.ToString());
    }

    //Return the appointment model back to the View.
    //The model will contain the ModelError, which will trigger
    // any validation code on the View.
    return View(appointment);
}

答案 1 :(得分:-3)

这是一种非常业余的编码方式,我建议你将它与带有if语句的java进行比较。您需要获取该值并将其发送到数据库并将其与值进行比较。但就像我说我会重新考虑这段代码,因为这是一种低效的编码方式而不是正确的方式。

goodluck