I trying to learm MVC .net core and I need to populate a combobox and save in to Database the ID of combobox.
I have this code in my controller:
ViewBag.Categories = new List<SelectListItem>
{
new SelectListItem{ Text="history", Value="1"},
new SelectListItem{ Text="literature", Value="2"},
};
here the code in my view:
<div class="form-group row">
<div class="col-3">
<label asp-for="IdCategory">Category</label>
</div>
<div class="col-6">
<select asp-for="IdCategory" name="CategoryId" asp-items="ViewBag.Categories">
<option>-- select the category --</option>
</select>
<span asp-validation-for="IdCategory" class="text-danger"></span>
</div>
</div>
The combobox show correctly the content but after I save in to the database the field "IdCategory" = 0.
My impression is that the view try to save "history" in field "IdCategory" and not "1" that is the ID.
If this is true, how I can force to save the ID and not the text ?
Thanks
Update here the controller:
public IActionResult Upsert(int? id)
{
ViewBag.Categories = new List<SelectListItem>
{
new SelectListItem{ Text="history", Value="1"},
new SelectListItem{ Text="literature", Value="2"},
};
Book = new Book();
if (id == null)
{
//create
return View(Book);
}
//update
Book = _db.Books.FirstOrDefault(u => u.Id == id);
if (Book == null)
{
return NotFound();
}
return View(Book);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert()
{
if (ModelState.IsValid)
{
if (Book.Id == 0)
{
//create
_db.Books.Add(Book);
}
else
{
_db.Books.Update(Book);
}
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(Book);
}
here the complete view:
@model BookListMVC.Models.Book
<br />
<h2 class="text-info">@(Model.Id!=0 ? "Edit" : "Create") Book</h2>
<br />
<div class="border container" style="padding:30px;">
<form method="post">
@if (Model.Id != 0)
{
<input type="hidden" asp-for="Id" />}
<div class="text-danger" asp-validation-summary="ModelOnly"></div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Name"></label>
</div>
<div class="col-6">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Author"></label>
</div>
<div class="col-6">
<input asp-for="Author" class="form-control" />
<span asp-validation-for="Author" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="ISBN"></label>
</div>
<div class="col-6">
<input asp-for="ISBN" class="form-control" />
<span asp-validation-for="ISBN" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="ISBN"></label>
</div>
<div class="col-6">
<input asp-for="ISBN" class="form-control" />
<span asp-validation-for="ISBN" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="IdCategory">Category</label>
</div>
<div class="col-6">
<select asp-for="IdCategory" name="CategoryId" asp-items="@ViewBag.Categories">
<option>-- select the category --</option>
</select>
<span asp-validation-for="IdCategory" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-3 offset-3">
<button type="submit" class="btn btn-primary form-control">
@(Model.Id != 0 ? "Update" : "Create")
</button>
</div>
<div class="col-3">
<a asp-action="Index" class="btn btn-success form-control">Back to List</a>
</div>
</div>
</form>
</div>
@section Scripts{
<partial name="_ValidationScriptsPartial" />
}
and the book class:
public class Book
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public int IdCategory { get; set; }
}