Inside a MVC controller I attempted to create a field similar to:
Func<MyModel, ViewResult> ModelResult=(model) => View("myview.cshtml", model);
This results in the compilation error
An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.View(string, object)'
This code works fine as a method
private ViewResult ModelResult(MyModel model)
{
return View("myview.cshtml", model);
}
It also works fine if the field is initialized by the constructor
public MyController()
{
ModelResult=(model) => View("myview.cshtml", model);
}
Why is the field initializer treated as a static context?