2010-07-25 3 views
0

앞으로 새로운 질문 :기본보기 모델 클래스 참조 질문. (ASP.net MVC)

내 View Model 선언은 어디에 두어야합니까?

내 모델 폴더에 "ViewModels.cs"라는 파일을 만들었습니다. 지금은 새로운 RatingViewModel 객체를 반환하는 도우미/서비스 클래스 기능을 만들려고 해요

using System; 
using System.Collections.Generic;var 
using System.Linq; 
using System.Web; 

namespace Web.Model 
{ 
public class ViewModels 
{ 

    public class RatingViewModel 
    { 
     public User Rater { get; set; } 
     public Rating Rating { get; set; } 
    } 

} 

} V

: 나는 하나라는 RatingViewModel을 포함하여 몇 가지보기 모델 클래스를 만들었습니다.

"RatingsHelpers.cs"파일을 만들었습니다. 그러나 새로운 RatingViewModel 객체를 만들려고 할 때, 내가 말하는 것에 대해 전혀 알지 못합니다. 나는 그것을 이해할 수 없다. 참조가 누락 되었습니까? 헬퍼/서비스 클래스에서 새 View Model 객체를 만들거나 반환하려면 어떻게해야합니까?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Text; 
using Web.Model; 

namespace System.Web.Mvc 
{ 
    public static class RatingsHelpers 
    { 
     public static RatingViewModel functionname() 
     { ..... } 

그러나 "RatingViewModel"이 무엇인지 알 수 없습니다 ...!?!?!?

나는 이것이 분명해야한다는 것을 알고 있습니다. 내 머리를 당기

,

조니

+0

자신의 네임 스페이스로 "System.Web.Mvc"를 사용할 수 있는지 확실하지 않습니다. – mxmissile

답변

2

요약하면 :

  1. 당신은 Web.Model 네임 스페이스 내부 ViewModels 클래스를 만들었습니다. 이 클래스는 RatingViewModel
  2. 중첩 된 클래스를 선언합니다 System.Web.Mvc 네임 스페이스에있는 도우미 RatingsHelpers 안에 RatingViewModel 클래스에 액세스하려고합니다.

ViewModels.RatingViewModel을 사용하면이 문제를 해결할 수 있지만 권장하지는 않습니다. 이런 중첩 클래스는 의미가 없습니다.

대신 별도의 .cs 파일로 각 뷰 모델 클래스를 배치 할 수 있고 Web.Model 네임 스페이스에 직접 넣어 :

namespace Web.Model 
{ 
    public class RatingViewModel 
    { 
     public User Rater { get; set; } 
     public Rating Rating { get; set; } 
    } 
} 

그런 다음 헬퍼 클래스의 사용은 직접 RatingViewModel 클래스를 사용할 수있는 내부.

+0

대단히 감사합니다! – johnnycakes

2

문제는 당신이 ViewModels의 하위 클래스의 것을 의미 ViewModels 클래스, 내부 RatingViewModel 클래스를 넣어 것입니다. 나는 그것이 당신이 원하는 행동이라고 확신하지 못합니다. 하위 클래스를 호출하려면 클래스를 참조하기 위해 ViewModels.RatingViewModel을 써야합니다.

2

scripni가 말한 것에 대해 확장했다. Web.Models 내부의 ViewModels 클래스 내에 RatingViewModel 클래스가 중첩되어있는 것은 불필요한 것처럼 보입니다. 아마 외부 ViewModels 클래스를 없애고 싶을 것이다.