控制器错误ErrorExtension方法必须在非通用静态类中定义

时间:2019-01-23 08:21:18

标签: c# asp.net-mvc

此错误是怎么回事?

“扩展方法必须在非通用静态类中定义”

控制器:

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);
        }
    }
}

我尝试将其更改为“静态”,但这会在我的所有操作中产生更多错误。

  

错误3'索引':无法在静态类中声明实例成员

上次我关闭项目时,一切工作正常,我只是打开它并运行并得到了这些错误。

1 个答案:

答案 0 :(得分:2)

问题是您在MVC控制器类中使用了静态MvcHtmlString方法,该方法不应与静态HTML帮助器一起使用(必须将控制器类声明为非静态)。尝试将自定义HTML帮助器放在其他静态类中:

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);
    }
}

然后,在Razor视图中添加对该类的引用,您以后可以调用它:

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

相关问题:

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