Reputation: 7818
I'm getting myself completely confused, and would appreciate if anyone can point me in the right direction again.
I am trying to add a search form to a ficticious project, for my own learning purposes.
Idea is - I have a Date text box, and a number of days hire text box - when search is hit - the code should look for all cars that have been booked between the date entered, and the date plus the number of days hire required. (A)
This will generate a list of cars which have to be excluded from the next part of the search (B)
It then searches for Cars (C), and excludes the list generated at (B)
The Linq then populates the ct object (D) freeCarTypes.
freeCarTypes at this point, correctly holds the data I'm looking for - a list of:
Car Type (TypeNme)
Type ID
TypeCount (how many of this car type are available)
NumSelected (set to 0 by default)
NumSelected (I'm hoping) will be populated in the model, when I list each car type, with a drop down list of the TypeCount (available) - which will then populate the NumSelected part of the model. so when the post happens, I have a list of car types, and the number of each type the Dealer is interested in.
My problem is, how do I get the List of cars - freeCarTypes (D) - into my SearchViewModel.TypesAvail?
Is it my SearchViewModel using IQueryable that is wrong, or should I be using something else here - I'm going round in circles.
Thanks for any help,
Mark
public class SearchViewModel
{
[Required]
public DateTime From { get; set; }
[Required]
public int Days { get; set; }
public long DealerID { get; set; }
public IQueryable<Car> Cars { get; set; }
public IQueryable<TypesAvail> TypesAvails { get; set; }
}
public class TypesAvail
{
public String TypeNme { get; set; }
public long TypeID { get; set; }
public int TypeCount { get; set; }
public int NumSelected { get; set; }
}
My SearchController
[HttpPost]
public ActionResult Avail(SearchViewModel model, string id)
{
if (ModelState.IsValid)
{
// (A)
var dteFrom = model.From;
var dteTo = model.From.AddDays(model.Nights);
// (B)
var prebookedCars = db.Cars
.Where(car => car.Rentals.Any(rental =>
(model.DealerID == rental.Dealer_id) && (
(dteFrom >= rental.dateout && dteFrom < rental.dateback)
||
(dteTo > rental.dateout && dteTo <= rental.dateback)
||
(dteFrom <= rental.dateout && dteTo >= rental.dateback)
)));
// (C)
var freeCarTypes = db.Cars
.Where(r => r.DealerID == model.DealerID)
.Except(prebookedCars)
.GroupBy(p => p.CarType)
.Select(ct => new // (D)
{
TypeNme = ct.Key.type_name,
TypeID = ct.Key.type_id,
TypeCount = ct.Count(),
NumSelected = 0
}
);
foreach (var fr in freeCarTypes)
{
TypesAvail ta = new TypesAvail();
{
ta.NumSelected = fr.NumSelected;
ta.TypeCount = fr.TypeCount;
ta.TypeID = fr.TypeID;
ta.TypeNme = fr.TypeNme;
}
}
// This is where I'm stuck - how do I get the list above, into the ViewModel, to I can generate a list of car types, with a drop down list of how many are available
// model.TypesAvail = ta;
// model.TypesAvail = freeCarTypes;
return View(model);
}
else // model is not valid, so return the View
{
return View(model);
}
}
Upvotes: 2
Views: 309
Reputation: 1823
How about this?
// (C)
model.TypesAvail = db.Cars.Where(r => r.DealerID == model.DealerID)
.Except(prebookedCars)
.GroupBy(p => p.CarType)
.Select(ct => new TypesAvail // (D)
{
TypeNme = ct.Key.type_name,
TypeID = ct.Key.type_id,
TypeCount = ct.Count(),
NumSelected = 0
};
Upvotes: 4