0

So when i scaffold a controller for my Event model it builds the typical GET/POST controller page.

It seems like every instance of the variable "event" has an @ in front of it. If i try to remove the @ it doesn't recognize the word as a variable (even with var in front of the word). Im pretty new to coding and i havent seen this before, what is causing this?

See examples below:

public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var @event = await _context.Events
                .FirstOrDefaultAsync(m => m.EventId == id);
            if (@event == null)
            {
                return NotFound();
            }

            return View(@event);
        }
public async Task<IActionResult> Create([Bind("EventId,Subject,Description,StartTime,EndTime,Theme,IsFullDay")] Event @event)
        {
            if (ModelState.IsValid)
            {
                _context.Add(@event);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(@event);
        }
elementmg
  • 234
  • 2
  • 14
  • 3
    how do you scaffold your controller? `event` is a special name (matching with the keyword `event`) so to make it valid, we need to prefix it with `@`. Check this out https://stackoverflow.com/questions/254669/what-does-placing-a-in-front-of-a-c-sharp-variable-name-do – King King Feb 09 '21 at 00:47

1 Answers1

2

event is a reserved word, if use it, you have to add @ in front of it. You can change it to other variable.

Here is all the Keywords.

Karney.
  • 4,803
  • 2
  • 7
  • 11