Код: Выделить всё
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 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");
}
< /code>
createlistingviewmodel.cs:
public class CreateListingViewModel
{
public IEnumerable AllCities { get; set; }
public AddressViewModel Address { get; set; }
public BuildingViewModel Building { get; set; }
public ApartmentViewModel Apartment { get; set; }
public IEnumerable AllItems { get; set; }
public IEnumerable Items { get; set; }
public TermsViewModel Terms { get; set; }
public ListingViewModel Listing { get; set; }
}
< /code>
_createform.cshtml:
Grad
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/796 ... id-for-som