【问题标题】:Controller error ErrorExtension method must be defined in a non-generic static class控制器错误 ErrorExtension 方法必须在非泛型静态类中定义
【发布时间】:2019-01-23 08:21:17
【问题描述】:

这个错误是怎么回事?

"扩展方法必须定义在非泛型静态类中"

控制器:

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

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

Error3 'Index': 不能在静态类中声明实例成员

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

【问题讨论】:

  • 您需要将DisplayWithBreaksFor 方法移动到其他静态类。 non-static类中不能定义扩展方法,Controller类不能是静态类,当你有动作方法时。

标签: c# asp.net-mvc


【解决方案1】:

问题是您在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 2016-10-25
    • 2023-03-10
    • 2014-12-23
    相关资源
    最近更新 更多