I'm currently working on a form where you can create a new car which gets saved into a list of cars in my database. However, when you create a car, you can also select a brand from a drop down list. I've inserted the values of the options from a table/model which is called Brands. Right now, you can choose a brand, however, when I click sumbit everything other than the brand gets saved.
Is it that I've written the wrong thing in the value attribute of the option tag or does the issue lie in something else?
Create.cshtml.cs
namespace CarRental.Pages.CarsList
{
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
public CreateModel(ApplicationDbContext db)
{
_db = db;
}
[BindProperty]
public Cars Car { get; set; }
public IList<Brands> Brands { get; set; }
public void OnGet()
{
// going inside the DB, retrievieng all the brands and storing them in the IList
Brands = _db.Brands.ToList();
}
public async Task<IActionResult> OnPost(Cars carObj)
{
if (ModelState.IsValid)
{
//add item in a que so that it can be pushed to the database
_db.Cars.AddAsync(Car);
//save changes to the database, aka push changes to the database
await _db.SaveChangesAsync();
//once the changes are pushed to the database
return RedirectToPage("Index");
} else
{
return Page();
}
}
}
}
Create.cshtml
@page
@model CarRental.Pages.CarsList.CreateModel
@{
ViewData["Title"] = "Create";
}
<h2 class="text-info">Create New Book</h2>
<br />
<div class="border container" style="padding:30px;">
<form method="post">
<div class="text-danger" asp-validation-summary="ModelOnly"></div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Car.LicensePlate"></label>
</div>
<div class="col-6">
<input asp-for="Car.LicensePlate" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Car.Brand"></label>
</div>
<div class="col-6">
<select asp-for="Car.Brand" class="form-control">
@foreach (var item in Model.Brands)
{
<option value=">@Html.DisplayFor(m => item.Brand)">@Html.DisplayFor(m => item.Brand)</option>
}
</select>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<label asp-for="Car.Price"></label>
</div>
<div class="col-6">
<input asp-for="Car.Price" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-3 offset-3">
<input type="submit" value="Create" class="btn btn-primary form-control" />
</div>
<div class="col-3">
<a asp-page="Index" class="btn btn-success form-control">Back to List</a>
</div>
</div>
</form>
@Html.ValidationSummary();
</div>
Brands.cs
namespace CarRental.Models
{
public class Brands
{
[Key]
public string Brand { get; set; }
}
}
My Cars.cs model, incase that's of interest
namespace CarRental.Models
{
public class Cars
{
[Key]
public string LicensePlate { get; set; }
public Brands Brand { get; set; }
[Required]
public float Price { get; set; }
[Required]
public bool StatusRented { get; set; } = true;
public DateTime? StartDate { get; set; }
}
}
Thank you in advance!