How to get values of selected items id in CheckBox in ASP.NET CORE?

315 views Asked by At

I have 3 table. first one for Movie , second one for Category, third one(MovieCategor) its hold MovieId and Category Id

I want to insert Movie and choose the category. I list to category like checkbox to page View. How to make insert for Movie and MovieCategor table?

View Page -

 <ul style="list-style-type: none">
                        @foreach (var category in Model.Categories)
                        {
                            <li>
                            <input type="checkbox" value="@category.Id" name="Id" class="form-check-input" id="exampleCheck1">
                            <label class="form-check-label" for="exampleCheck1">@category.CategoryName</label>
                            </li>
                        }
                    </ul>

Controller -

public IActionResult Create(MovieCreateViewModel movieCreateViewModel,int[] Id)
        {
            if (ModelState.IsValid)
            {
                string fileName = UploadFile(movieCreateViewModel);
                Movie movie = new Movie()
                {
                    Name = movieCreateViewModel.Name,
                    Director = movieCreateViewModel.Director,
                    Summary = movieCreateViewModel.Summary,
                    Banner = fileName,
                    MoviesCategory = movieCreateViewModel.MoviesCategories
                };
                foreach (var id  in Id)
                {
                    MoviesCategory moviesCategory = new MoviesCategory();

                    moviesCategory.CategoryId = id;
                    moviesCategory.MovieId = movie.Id;
                    
                    _movieCategoryService.Add(moviesCategory);
                }
                _movieService.Add(movie);
                return RedirectToAction("List");
            }

            return View();
        }
}
0

There are 0 answers