0

What's is going on with this error?

"Extension method must be defined in a non-generic static class"

Controller:

namespace HolidayTracker.Controllers
{
    public class HolidayRequestFormsController : Controller
    {
        private LotusWorksEntities db = new LotusWorksEntities();

        // GET: HolidayRequestForms
        public ActionResult Index()
        {
            var holidayRequestForms = db.HolidayRequestForms.Include(h => h.Employee);
            return View(holidayRequestForms.ToList());
        }

        // GET: HolidayRequestForms/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HolidayRequestForm holidayRequestForm = db.HolidayRequestForms.Find(id);
            if (holidayRequestForm == null)
            {
                return HttpNotFound();
            }
            return View(holidayRequestForm);
        }

        // GET: HolidayRequestForms/Create
        public ActionResult Create()
        {
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName");
            return View();
        }

        // POST: HolidayRequestForms/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "RequestID,EmployeeID,StartDate,FinishDate,HoursTaken,Comments,YearCreated,MonthCreated,DayCreated,YearOfHoliday,Approved")] HolidayRequestForm holidayRequestForm)
        {
            if (ModelState.IsValid)
            {
                db.HolidayRequestForms.Add(holidayRequestForm);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName", holidayRequestForm.EmployeeID);
            return View(holidayRequestForm);
        }

        // GET: HolidayRequestForms/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HolidayRequestForm holidayRequestForm = db.HolidayRequestForms.Find(id);
            if (holidayRequestForm == null)
            {
                return HttpNotFound();
            }
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName", holidayRequestForm.EmployeeID);
            return View(holidayRequestForm);
        }

        // POST: HolidayRequestForms/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "RequestID,EmployeeID,StartDate,FinishDate,HoursTaken,Comments,YearCreated,MonthCreated,DayCreated,YearOfHoliday,Approved")] HolidayRequestForm holidayRequestForm)
        {
            if (ModelState.IsValid)
            {
                db.Entry(holidayRequestForm).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FullName", holidayRequestForm.EmployeeID);
            return View(holidayRequestForm);
        }

        // GET: HolidayRequestForms/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HolidayRequestForm holidayRequestForm = db.HolidayRequestForms.Find(id);
            if (holidayRequestForm == null)
            {
                return HttpNotFound();
            }
            return View(holidayRequestForm);
        }

        // POST: HolidayRequestForms/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            HolidayRequestForm holidayRequestForm = db.HolidayRequestForms.Find(id);
            db.HolidayRequestForms.Remove(holidayRequestForm);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public static MvcHtmlString DisplayWithBreaksFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            var model = html.Encode(metadata.Model).Replace("\r\n", "<br />\r\n");

            if (String.IsNullOrEmpty(model))
                return MvcHtmlString.Empty;

            return MvcHtmlString.Create(model);
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

I've tried changing it to Static but that creates more errors with all my actions.

Error3 'Index': cannot declare instance members in a static class

Everything was working fine last time I closed the project, I just opened it and ran and got these errors.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Conor8630
  • 345
  • 1
  • 17
  • You need to move `DisplayWithBreaksFor` method to some other static class. You can not define extension methods in `non-static` class and Controller class can not be a static class when you have action methods in it. – Chetan Jan 23 '19 at 08:30

1 Answers1

2

The problem is you're using static MvcHtmlString method inside MVC controller class, which not supposed to be used with static HTML helpers (the controller class must be declared as non-static). Try putting the custom HTML helper inside other static class instead:

public static class HtmlHelpers
{
    public static MvcHtmlString DisplayWithBreaksFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        var model = html.Encode(metadata.Model).Replace("\r\n", "<br />\r\n");

        if (String.IsNullOrEmpty(model))
            return MvcHtmlString.Empty;

        return MvcHtmlString.Create(model);
    }
}

Then, add reference to that class in Razor view and you can call it later:

@Html.DisplayWithBreaksFor(model => model.SomeProperty)

Related issue:

Error :Extension method must be defined in a non-generic static class

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61