Hi,
In my database I have Offers, Levels(basic, intermediate, expert) and OfferLevels tables. One offer can have zero or multiple Levels.
I can create website which will create new offer but I cant figure out how to write levels to OfferLevels table.
I want to display levels as 3 checkboxes.
Could somebody help me with how to write controller, viewmodel(hold offer and levels objects) and view with asp.net core?
My code:
Controller public IActionResult Create() { var viewModel = new ViewModel(); return View(viewModel); } [HttpPost] [ValidateAntiForgeryToken] public IActionResult Create(ViewModel viewModel) { if (ModelState.IsValid) { viewModel.Offer.OfferDate = DateTime.Now.ToLocalTime(); context.Offers.Add(viewModel.Offer); context.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel.Offer); } ViewModel public partial class ViewModel { public Offer Offer { get; set; } public List<Level> Levels { get; set; } } View <form asp-controller="Home" asp-action="Create" method="post" class="form-horizontal" role="form"><div asp-validation-summary="All" class="text-danger"></div><div class="form-group"><label asp-for="Offer.OfferTitle"></label><input asp-for="Offer.OfferTitle" class="form-control" /><span asp-validation-for="Offer.OfferTitle" class="text-danger"></span></div> ....there should be checboxes dont how to put them here.... <div class="form-group"><input type="submit" value="Create" class="btn btn-default" /></div></form>