What I’m trying to do is to get particular values from my DB and parse that into view to display it in the table.
I had same problem as in The model item passed into the dictionary is of type …. {Models.App} but what I passed is {Models.App, System.Double} and tried to solve my problem in the similar way. So basically I created a class in the model that has:
public Vehicle manur_name {get; set;}
public Vehicle manur_date { get; set; }
public Vehicle daily_hire_rate { get; set; }
but then when I use it in my controller in the linq query it gives me and error.
var s = from veh in db.Vehicles
join c in db.VehicleCategories on veh.vehicle_category_code equals c.
join m in db.Models on veh.model_code equals m.model_code
join man in db.Manufacturers on veh.manufacturer_code equals man.manufacturer_code
where c.vehicle_category_description == cat && m.body_style == b
select new CarClass {
manur_name = man.manufacturer_name,
manur_date = m.model_code,
daily_hire_rate = veh.daily_hire_rate};
It gives me the error:
error:Cannot implicitly convert type ‘decimal’ to
‘carRentalMVC.Models.Vehicle’ Cannot implicitly convert type ‘string’
to ‘carRentalMVC.Models.Vehicle’ Cannot implicitly convert type
‘string’ to ‘carRentalMVC.Models.Vehicle’
Here is what I have in controller after fixing the problem:
CarRentalDatabaseDataContext db = new CarRentalDatabaseDataContext();
public ActionResult Index(String cat, String b, String sort)
{
String day_hire = "Daily hire rate";
String man_date = "Manufacturing date";
string man_n = "Manufacturer's name";
var s = db.Vehicles.Where(c => c.Model.body_style == b)
.Select(u => new CarClassViewModel {
manur_name = u.Manufacturer.manufacturer_name,
model_code = u.model_code,
daily_hire_rate = u.daily_hire_rate,
manur_date = u.manufacturing_date
})
.Distinct(); //I don't need repeating data
//this is for sorting them, but I haven't implemented this in the view yet.
if (sort == man_n){s = s.OrderBy(c=>c.manur_name);}
else if (sort == man_date){s = s.OrderBy(c =>c.manur_date);}
else if (sort == day_hire){s = s.OrderBy(c => c.daily_hire_rate);}
else{s = s.OrderBy(c => c.daily_hire_rate);}
var v = s.Select(u => new CarClassViewModel
{
manur_name = u.manur_date,
model_code = u.model_code,
daily_hire_rate = u.daily_hire_rate,
}); //this is for returning values I need
return View(v); //I've tried View(v.ToList()) as well
}
Now this returns empty rows in my table. Does it matter that I’m not parsing in data for the method?
Your data model is very messed up. Even leaving the naming conventions aside, look at your properties:
The manufacturer name of a car isn’t a vehicle – it’s likely to be a string. The manufacturing date of a car isn’t a vehicle – it’s likely to be a
DateTime. The daily hire rate of a car isn’t a vehicle – it’s likely to be adecimal. So your model should look like this:Get your data model right, and everything else should become a lot simpler. Having said that, this looks pretty suspicious too:
A “date” and a “code” don’t sound like the same thing to me.