ListingController.cs:
public IActionResult Create()
{
CreateListingViewModel model = new()
{
AllCities = new SelectList(_addressRepo.AllCities()),
AllItems = new SelectList(_itemRepo.FindAll(), "Id", "Name")
};
return View(model);
}
[HttpPost]
public IActionResult Create(CreateListingViewModel model)
{
if (!ModelState.IsValid)
{
foreach (var key in ModelState.Keys)
Console.WriteLine($"{key}: {ModelState.GetValidationState(key)}");
model.AllCities = new SelectList(_addressRepo.AllCities());
model.AllItems = new SelectList(_itemRepo.FindAll(), "Id", "Name");
return View(model);
}
Address address = _addressRepo.FindOrCreate(model.Address);
Building building = _buildingRepo.FindOrCreate(address, model.Building);
List<Item> items = _itemRepo.FindByIdIn(model.Items);
Apartment apartment = _apartmentRepo.FindOrCreate(building, items, model.Apartment);
Terms terms = _termsRepo.FindOrCreate(model.Terms);
_listingRepo.Create(apartment, terms, model.Listing);
return RedirectToAction("Index");
}
CreateListingViewModel.cs:
public class CreateListingViewModel
{
public IEnumerable<SelectListItem> AllCities { get; set; }
public AddressViewModel Address { get; set; }
public BuildingViewModel Building { get; set; }
public ApartmentViewModel Apartment { get; set; }
public IEnumerable<SelectListItem> AllItems { get; set; }
public IEnumerable<long> Items { get; set; }
public TermsViewModel Terms { get; set; }
public ListingViewModel Listing { get; set; }
}
_CreateForm.cshtml:
<div class="row mb-3">
<label class="col-sm-2 col-form-label" asp-for="Address.City">Grad</label>
<div class="col-sm-4">
<select class="form-select" asp-for="Address.City" asp-items="Model.AllCities"></select>
</div>
</div>
I cannot pass form validation, because AllItems and AllCities, forms that are used for populating select dropdowns, are invalid. Like what?
Items: Valid
AllItems: Invalid
AllCities: Invalid
Terms.Deposit: Valid
Terms.ForWorkers: Valid
Terms.ForStudents: Valid
Terms.PetsAllowed: Valid
Terms.DateAvailable: Valid
Terms.SmokingAllowed: Valid
Address.City: Valid
Address.Street: Valid
Address.StreetNum: Valid
Listing.Title: Valid
Listing.Description: Valid
Building.Cctv: Valid
Building.Garage: Valid
Building.Parking: Valid
Building.Elevator: Valid
Building.Intercom: Valid
Building.NumOfFloors: Valid
Building.YearConstructed: Valid
Apartment.Area: Valid
Apartment.Floor: Valid
Apartment.Price: Valid
Apartment.State: Valid
Apartment.Heating: Valid
Apartment.Equipment: Valid
Apartment.NumOfRooms: Valid
Here's the project: https://github.com/IgorArnaut/ASP-NET-Validation-Problem. These two are not supposed to be validated at all. They just populate dropdowns.