Expression does not have a name CS8081

Brent Humber 21 Reputation points
2021-03-16T15:31:47.467+00:00

I getting this as a error, I get it??? this is in the controller.

public class CorvetteCostController : Controller
{
private const double V = 99.00;

    // GET: CorvetteCostController
    public ActionResult GetIndex()
    {
        return View();
    }

    public IActionResult Index
    {
        get
        {
            var model = new List<CorvetteCost>()
        {
            new CorvetteCost{ CorvetteId = 1, CorvetteItem = "Filter", CorvetteItemCost = "$99.00", CorvetteStore = "Corvette Mods", Date = "3/3/2021", Maintance = "No", OrginalCost = (float)V, Repair = "yes", Upgrade = "no" }
            //new MyModel{ CorvetteId=1,CorvetteItem="aa"},
            // new MyModel{ CorvetteId=2,CorvetteItem="bb"}
        };
            return View(model);
        }
    }

    // GET: CorvetteCostController/Details/5
    public ActionResult Details(int id)
    {
        return View();
    }

    // GET: CorvetteCostController/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: CorvetteCostController/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(IFormCollection collection)
    {
        try
        {
            return RedirectToAction(nameof(GetIndex()));  //GetIndex is the error
        }
        catch
        {
            return View();
        }
    }

    // GET: CorvetteCostController/Edit/5
    public ActionResult Edit(int id)
    {
        return View();
    }

    // POST: CorvetteCostController/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(int id, IFormCollection collection)
    {
        if (collection is null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        try
        {
            return RedirectToAction(nameof(GetIndex())); //GetIndex is the error
        }
        catch
        {
            return View();
        }
    }

    // GET: CorvetteCostController/Delete/5
    public ActionResult Delete(int id)
    {
        return View();
    }

    // POST: CorvetteCostController/Delete/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(int id, IFormCollection collection)
    {
        try
        {
            return NewMethod();
        }
        catch
        {
            return View();
        }

        ActionResult NewMethod()
        {
            return RedirectToAction(nameof(GetIndex())); //GetIndex is the error
        }
    }
}

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,319 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,976 Reputation points
    2021-03-16T16:32:47.47+00:00

    nameof requires an identifier. You are trying to call the function GetIndex() and whatever that returns (the expression) would then be passed to nameof. nameof is a compile time expression and just needs the identifier name.

    return RedirectToAction(nameof(GetIndex));  // => RedirectToAction("GetIndex")
    
    0 comments No comments