2012-09-22 3 views
0

익명 형식에 형식을 내 DB에서 특정 값을 가져 와서 테이블에 표시하려면보기로 구문 분석 할 것입니다. 나는 The model item passed into the dictionary is of type .... {Models.App} but what I passed is {Models.App, System.Double}에서와 같은 문제가 있었고 비슷한 방식으로 내 문제를 해결하려고 노력했습니다.linq 쿼리

public Vehicle manur_name {get; set;} 
public Vehicle manur_date { get; set; } 
public Vehicle daily_hire_rate { get; set; } 

하지만 난 LINQ 쿼리에 내 컨트롤러에서 사용할 때 그 때 저와 오류를 제공을 : 그러니까 기본적으로 나는이 모델에서 클래스를 만들었습니다. 그것은 나에게 오류 제공

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}; 

: 여기

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'

문제를 해결 한 후 내가 컨트롤러에있는 것입니다 :

 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 
    } 

지금이 내 테이블에 빈 행을 반환합니다. 방법에 대한 데이터를 파싱하지 않아도되는 것이 중요합니까?

답변

4

I'm not sure what I'm doing wrong.

데이터 모델이 매우 엉망입니다. 심지어 옆으로 명명 규칙을 떠나, 당신의 재산을보고 :

public Vehicle manur_name {get; set;} 
public Vehicle manur_date { get; set; } 
public Vehicle daily_hire_rate { get; set; } 

자동차 의 제조업체 이름이 차량 아니다 - 문자열이 될 가능성이 높습니다. 차량 제조일 은 차량 번호이 아니며 DateTime 일 가능성이 큽니다. 차량 의 일일 대여료는 차량이이 아니며, decimal 일 가능성이 큽니다. 그래서 모델은 다음과 같아야합니다

public string ManufacturerName { get; set; } 
public DateTime ManufacturingDate { get; set; } 
public decimal DailyHireRate { get; set; } 

는 데이터 모델의 권리를 취득하고, 다른 모든 것이 많은 간단하게 될 것이다.

manur_date = m.model_code 

A "날짜"와 "코드가"나에게 같은 일을 같은 소리하지 않습니다, 이것은 꽤 의심스러운 가졌어요.

+0

)) 변수에 할당 된 유형이 올바르지 않다는 것을 알아 냈습니다. 이제는 내 코드의 모든 내용이 컴파일되지만 실제로 아무 것도 반환하지 않습니다. 어떻게 해석해야합니까? –

+0

@ NurkaKindova : 당신이 뭘 잘못하고 있는지 알기가 어렵습니다. 새로운 질문에 대해 더 자세히 알려주십시오. (당신은 거기에 많은 조인과 필터링을 가지고 있는데, 그 부분은 원치 않는 데이터를 걸러 낼 수 있습니다.) –

+0

좋아, 질문을 수정하고 현재 가지고있는 코드를 추가했습니다. 좀 봐 주시겠습니까? –