非静态字段,方法或属性需要对象引用

时间:2015-07-08 19:36:23

标签: c# asp.net-mvc

我看过像这样的重复错误,但我还没找到解决方案

这是我在业务逻辑中实现的方法

  public  void UpdateBooking(BookingView model)
    {
        using (var booking = new BookingRepository())
        {
            var user = new ApplicationUser();
            Booking book = booking.GetById(model.BookingId);

            if (book != null)
            {

                book.BookingId = model.BookingId;
                book.BookingDate = model.BookingDate;
                book.BookingTime = model.BookingTime;
                book.Location = model.Location;
                //book.Status = DefaultStatus();
                //book.TreatmentName = book.TreatmentName;
                //book.AddInfo = model.AddInfo;

                booking.Update(book);
            }
        }
    }

但我的错误在于我的预订控制器方法

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult postponeBooking([Bind(Include = "location,BookingDate,BookingTime")]BookingView model)
    {

        if (ModelState.IsValid)
        {

            BookingBusiness.UpdateBooking(model);

        }
        return RedirectToAction("BookingDetails", TempData["alertMessage"] = "<script> alert('Booking details saved!!');</script>");
    }

正好在这一行

            BookingBusiness.UpdateBooking(model);

我完全无能为力我尝试在我的方法中添加静态但仍然没有运气。可能我不能正确理解错误我需要提升指导 谢谢

1 个答案:

答案 0 :(得分:0)

要使用UpdateBooking方法,您的BookingBusiness类或UpdateBooking方法必须是静态的(您不会引用单个实例对象)或者您必须实例化一个对象类似BookingBusiness的类似(显然取决于你打算如何实现这些):

BookingBusiness yourObject = new BookingBusiness(/*constructor args*/);  

然后,您可以通过yourObject.UpdateBooking(model);

调用您的方法