Возникли проблемы после создания контроллера для моей модели.вот основная модель:
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; }
}
здесь другие модели, такие как Подкатегория, Место и Организатор, имеют привязку один ко многим (событиям).
Вот место, например:
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; }
}
, поэтому у меня есть коллекция событий в каждом месте.И есть контроллер событий
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);
}
}
Где каждый @ => @. Place throw 2 ошибки: CS0119 'Place' - это тип, который недопустим в данном контексте EventsPortal CS1646 Ключевое слово, идентификатор или строкаожидается после стенографического спецификатора: @
Все в одном контексте