Hello,
I have mange code on way that from ViewBag I fill select list, but when I post form every value is catch expect value from select list.
Controller
public IActionResult Create(int? id) { ViewBag.ItemTypes = _repositoryItemType.GetAll(); ViewBag.MachineTypes = _repositoryMachineType.GetAll(); if (id.HasValue) { var items = _repository.GetAll(w => w.Id == id, includes); return PartialView("_CreateItem", Mapper.Map<IEnumerable<ItemTypeViewModel>>(items)); } else { ItemViewModel itemViewModel = new ItemViewModel(); return PartialView("_CreateItem", itemViewModel); } } [HttpPost] public async Task<IActionResult> Create(int? id, [FromForm]ItemViewModel viewModel) { try { if (ModelState.IsValid) { bool isNew = !id.HasValue; var newItem = Mapper.Map<Item>(viewModel); newItem.MachineType.Name = _repositoryMachineType.Get(w => w.Id == viewModel.MachineType.Id).Name; newItem.ItemType.Name = _repositoryItemType.Get(w => w.Id == viewModel.ItemType.Id).Name; if (isNew) { newItem.CreateDate = DateTime.Now; var exist = _repository.Get(w => string.Equals(w.InternalCode, viewModel.InternalCode, StringComparison.CurrentCultureIgnoreCase)); if (exist != null) { TempData["item"] = "Item already exist."; } else { _repository.Insert(newItem); } } else { newItem.UpdateDate = DateTime.Now; _repository.Update(newItem); } if (await _repository.SaveChangesAsync()) { TempData["item"] = "Item successfully created."; } } } catch (Exception ex) { _logger.LogError(ex.Message); } return RedirectToAction("Index"); }
View
@model PDM.ViewModels.ItemViewModel @using PDM.Code @{ ViewData["Title"] = "Create New Item"; }<form role="form" method="post" asp-action="Create"> @await Html.PartialAsync("_ModalHeader", new ModalHeader { Heading = string.Format("{0} Item", Model.Id == 0 ? "Add" : "Edit") })<div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="modal-body"><div class="form-group"><label asp-for="Id"></label><input class="form-control" asp-for="Id" disabled /></div><div class="form-group"><label asp-for="InternalCode"></label><input type="text" asp-for="InternalCode" class="form-control" /><span asp-validation-for="InternalCode" class="text-danger"></span></div><div class="form-group"><label asp-for="Name">Name On Item</label><input type="text" asp-for="Name" class="form-control" /><span asp-validation-for="Name" class="text-danger"></span></div><div class="form-group"><label asp-for="Description"></label><input type="text" asp-for="Description" class="form-control" /></div><div class="form-group"><label asp-for="Enter"></label><input asp-for="Enter" class="form-control" /><span asp-validation-for="Enter" class="text-danger"></span></div><div class="form-group"><label asp-for="Exit"></label><input asp-for="Exit" class="form-control" /><span asp-validation-for="Exit" class="text-danger"></span></div><div class="form-group"><label asp-for="Thickness"></label><input asp-for="Thickness" class="form-control" /><span asp-validation-for="Thickness" class="text-danger"></span></div><div class="form-group"><label asp-for="Band"></label><input asp-for="Band" class="form-control" /><span asp-validation-for="Band" class="text-danger"></span></div><div class="form-group"><label asp-for="Elastic"></label><input asp-for="Elastic" class="form-control" /></div><div class="form-group"><label asp-for="Color"></label><input asp-for="Color" class="form-control" /><span asp-validation-for="Color" class="text-danger"></span></div><div class="form-group"><label asp-for="MadeBy"></label><input asp-for="MadeBy" class="form-control" /><span asp-validation-for="MadeBy" class="text-danger"></span></div><div class="form-group"><label asp-for="MachineType"></label><select class="form-control" asp-for="MachineType" as asp-items="@(new SelectList(ViewBag.MachineTypes,"Id","Name"))"></select></div><div class="form-group"><label asp-for="ItemType"></label><select class="form-control" asp-for="ItemType" as asp-items="@(new SelectList(ViewBag.ItemTypes,"Id","Name"))"></select></div></div> @await Html.PartialAsync("_ModalFooter", new ModalFooter { })</form>
As you can see from bellow everything is okay from HTML side
And on the end when I post, I'm missing those two values from select lists,why ?