0

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();
        }
}
pederli
  • 1
  • 1
  • they'll map to bools. Include your Movie and MovieCategory models. If I understand correctly you may want a many-to-many relationship here. – pcalkins Oct 16 '20 at 22:18
  • Yes I did many-to-many relationship but how to get Category Id on view page ? So if I make checkbox button of value ıd can I get the category Id ? – pederli Oct 17 '20 at 10:52
  • Check this post's answer: https://stackoverflow.com/questions/37778489/how-to-make-check-box-list-in-asp-net-mvc/37779070 In your case the values would be the ids. So you'll get back a List of categoryIDs which you can iterate through to make the database entries. (I believe any unchecked values will not be returned... asp.net sort of ignores false bools in forms...) – pcalkins Oct 17 '20 at 17:56
  • 1
    @pcalkins thank you for your helping.. I figure out and. it's worked :) – pederli Oct 17 '20 at 22:09

0 Answers0