1

Have trouble after creating controller to my model. here is main model:

public class Event
{
    public int ID { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    [DataType(DataType.Date)]
    [Required]
    public DateTime Date { get; set; }

    public Subcategory Subcategory { get; set; }
    public int SubcategoryID { get; set; }

    public Place Place { get; set; }
    public int PlaceID { get; set; }

    public Organisator Organisator { get; set; }
    public int OrganisatorID { get; set; }

    public ICollection<User_link> User_link { get; set; }
}

here other models like Subcategory, Place and Organisator have one to many(events) binding.

here is Place for example:

public class Place
{
    public int ID { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Call_number { get; set; }
    public string Email { get; set; }
    public string Vk { get; set; }
    public string Facebook { get; set; }

    public ICollection<Event> Event { get; set; }
}

so i have collection of events in each place. And there is events controller

public class EventsController : Controller
{
    private readonly ApplicationDbContext _context;

    public EventsController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: Events
    public async Task<IActionResult> Index()
    {
        var applicationDbContext = _context.Event.Include(@ => @.Organisator).Include(@ => @.Place).Include(@ => @.Subcategory);
        return View(await applicationDbContext.ToListAsync());
    }

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

        var @event = await _context.Event
            .Include(@ => @.Organisator)
            .Include(@ => @.Place)
            .Include(@ => @.Subcategory)
            .SingleOrDefaultAsync(m => m.ID == id);
        if (@event == null)
        {
            return NotFound();
        }

        return View(@event);
    }

    // GET: Events/Create
    public IActionResult Create()
    {
        ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title");
        ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address");
        ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title");
        return View();
    }

    // POST: Events/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 async Task<IActionResult> Create([Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
    {
        if (ModelState.IsValid)
        {
            _context.Add(@event);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
        ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
        ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
        return View(@event);
    }

    // GET: Events/Edit/5
    public async Task<IActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
        if (@event == null)
        {
            return NotFound();
        }
        ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
        ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
        ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
        return View(@event);
    }

    // POST: Events/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 async Task<IActionResult> Edit(int id, [Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
    {
        if (id != @event.ID)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(@event);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EventExists(@event.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
        ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
        ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
        return View(@event);
    }

    // GET: Events/Delete/5
    public async Task<IActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var @event = await _context.Event
            .Include(@ => @.Organisator)
            .Include(@ => @.Place)
            .Include(@ => @.Subcategory)
            .SingleOrDefaultAsync(m => m.ID == id);
        if (@event == null)
        {
            return NotFound();
        }

        return View(@event);
    }

    // POST: Events/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id)
    {
        var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
        _context.Event.Remove(@event);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

    private bool EventExists(int id)
    {
        return _context.Event.Any(e => e.ID == id);
    }
}

Where every @=>@.Place throw 2 errors: CS0119 'Place' is a type, which is not valid in the given context EventsPortal CS1646 Keyword, identifier, or string expected after verbatim specifier: @

Everything in one context

uldrsk
  • 93
  • 1
  • 10

1 Answers1

1

In C#, @ is verbatim specifier which is used with multi line string expressions to ignore escape characters and line breaks.

Just replace it with some valid identifier i.e. 'x', 'a' in lambda expressions, it should work.

Here is the example of Multiline String Literal in C# with use of @.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32