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



这个错误是怎么回事?

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

控制器:

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("rn", "<br />rn");
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":无法在静态类中声明实例成员

上次我关闭项目时,一切都很好,我只是打开它并运行,结果出现了这些错误。

问题是在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("rn", "<br />rn");
if (String.IsNullOrEmpty(model))
return MvcHtmlString.Empty;
return MvcHtmlString.Create(model);
}
}

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

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

相关问题:

错误:必须在非通用静态类中定义扩展方法

最新更新