0

나는 몇 시간 동안이 작업을 수행하려고 노력했습니다. 모델이라는 두 개의 모델에 Model이라는 모델이 있는데, 모델에서 데이터를 가져 오려고합니다. 뷰에 표시 할 모델, 하나의 모델 문만 뷰에 적용 할 수 있다는 것을 알고 있습니다. 그래서 내가보기에서 참조하고 싶습니다 속성을 포함하는 ViewModels을 만들었습니다. 지금은 컴파일을 받고 메신저를 실행할 때 오류 :은 ASP.NET 내부에 두 개의 모델 속성을 추가하는 방법입니다.

"컴파일러 오류 메시지 : CS0246 : 형식 또는 네임 스페이스 이름 '모델'을 찾을 수 없습니다 (사용 지시문이나 어셈블리 참조가 누락 되었습니까?) "

내가 할 수있는 다른 방법이 있다면 조언 해 주시면 감사하겠습니다.

새로운 뷰 모델 내 내부보기

public class MainModelscs <T> where T :class 
{ 
    public StoreAudit StoreAudit { get; set; } 
    public StoreQuestions StoreQuestions { get; set; } 
    public List<string> StoreWindow { get; set; } 

    public IPagedList<T> IndexList { get; set; } 

} 

뷰 모드의 특성,

@model PopMarketing.ViewModel.MainModelscs<PopMarketing.Models> 

모델 1

public class StoreQuestions 
{ 

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int ReviewId { get; set; } 

    public int AuditId { get; set; } 

    public int QuestionOne { get; set; } 

    public string QuestionTwo { get; set; } 
    public string QuestionThree { get; set; } 
    public string QuestionFour { get; set; } 

    } 
을 생성

모델 2

[Key] 
    [Column(Order = 1)] 
    public int AuditId { get; set; } 

    public string Date { get; set; } 

    public int StoreNumber { get; set; } 
    public string StoreName { get; set; } 

    public string Address { get; set; } 
    public string Manager { get; set; } 

컨트롤러 방법

public class AuditController : Controller 
{ 
    private PopMarketingContext db = new PopMarketingContext(); 

    // 
    // GET: /Audit/ 

    private const string PASSWORD = "MarchJava2016"; 

    public ActionResult Index(string searchString) 
    { 
     int number; 
     int check; 
     bool result = Int32.TryParse(searchString, out number); 

     if (result) 
     { 
      check = Convert.ToInt32(searchString); 

      var shops = from s in db.StoreAudit 
         select s; 

      if (!String.IsNullOrEmpty(searchString)) 
      { 
       shops = shops.Where(s => s.StoreName.ToUpper().Contains(searchString.ToUpper()) || 
        s.StoreNumber.Equals(check)); 
      } 

      return View(shops.ToList()); 
     } 

     else 
     { 
      var shops = from s in db.StoreAudit 
         select s; 

      if (!String.IsNullOrEmpty(searchString)) 
      { 
       shops = shops.Where(s => s.StoreName.ToUpper().Contains(searchString.ToUpper())); 
      } 

      return View(shops.ToList()); 
     } 


    } 
+0

컨트롤러와 뷰 모델에서 (부분적으로) 결합하려는 두 모델을 보여줄 수 있습니까? – Devilscomrade

+0

whta는 PopMarketing.Models이고 Models의 전체 네임 스페이스는 무엇입니까? – Nirman

+0

PopMarketing.Models를 전달해야합니다. 에서 @model PopMarketing.ViewModel.MainModelscs

답변

0

Jaimin는 @model에 당신은 당신이 클래스의 모델이 아닌 디렉토리 또는 네임 스페이스로 사용할 클래스를 통과, 맞습니다. 제대로 viewmodels을 사용으로

:

class Student 
{ 
public string name; 
public string mentor; 
} 

class Teacher 
{ 
public string name; 
public string salary; 
} 

그리고 내가보기 쇼 모두 이름을 갖고 싶어 :

이의 내가 두 개의 클래스가 있다고 가정 해 봅시다.

class StudentAndTeacherVM 
{ 
public Teacher teacher; 
public Student student; 
} 

컨트롤러는 다음과 같이 보일 것이다 : Model.student.name @

:

이제
public ActionResult Index() 
{ 
var student = new Student(); 
*fill student* 
var teacher = new Teacher(); 
*fill teacher* 
var model = new PersonVM(); 
model.teacher = teacher; 
model.student = student; 
return view(model); 
} 

당신이하여보기에서 도달 할 수있는 나는이 같은 뷰 모델을 만들 수 있습니다 @ Model.teacher.name

또는 가능한 한 효율적 이길 원한다면 VM에 이름을 넣을 수도 있습니다.

컨트롤러 :

public ActionResult Index() 
{ 
var student = new Student(); 
*fill student* 
var teacher = new Teacher(); 
*fill teacher* 
var model = new PersonVM(); 
model.teacherName = teacher.name; 
model.studentName= student.name; 
return view(model); 
} 

보기 :

class StudentAndTeacherVM 
{ 
public string teacherName; 
public string studentName; 
} 
0

하는 액션

에 MainModelscs의 인스턴스를 만듭니다. . .

i.e. MainModelscs <string> Obj = new MainModelscs <string> ; 

. . .

그런 다음보기로 전달하십시오. 보기에 지금

return View(Obj); 

0

죄송합니다

@model PopMarketing.ViewModel.MainModelscs<string> 

(모델 이름으로하지 네임 스페이스를 T을!)

, 내가 더 늦게 내가 말했다보다 얻었다. 그러나 두 테이블을 결합하여 모델에 매핑하고 뷰로 전달하는 방법은 다음과 같습니다. 속성이 데이터베이스의 주석 테이블에 해당하는 클래스의 첫 번째 클래스입니다.

public class Comments 
{ 
    public int ID { get; set; } 
    public string CommentText { get; set; } 
    public string Username { get; set; } 

    public int Upvote { get; set; } 
    public int Downvote { get; set; } 
    public int InappropriateFlags { get; set; } 
    public int PostId { get; set; } 
    public string ControllerName { get; set; } 
    public int ReplyTo { get; set; } 
    public bool replied { get; set; } 
} 

이제 주석 테이블과 조인해야하는 다른 사용자 프로필 테이블.

public class UserProfile 
{ 
    public int ID { get; set; } 
    public string Username { get; set; } 
    public string DisplayName { get; set; } 
    public int Reputation { get; set; } 
    public DateTime DateOfRegistration { get; set; } 
    public int Money { get; set; } 
    public string ImageUrl { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Country { get; set; } 
    public string State { get; set; } 
    public string City { get; set; } 
    public string Location { get; set; } 
    public int? PostalCode { get; set; } 
    public int ProfileViews { get; set; } 
    public string AboutMe { get; set; } 
} 

이제 두 테이블의 조인을 처리하는 데 필요한 모델입니다. 나는 두 테이블의 모든 속성을보기에 필요로하지 않았으므로 포함하지 않았습니다.하지만 포함시킬 수는 있습니다. 이 경우 데이터 액세스 쿼리도 업데이트해야합니다.

public class CommentUserProfileJoin 
{ 
    public int ID { get; set; } 
    public string CommentText { get; set; } 
    public string Username { get; set; } 
    public string DisplayName { get; set; } 
    public int Upvote { get; set; } 
    public int Downvote { get; set; } 
    public int InappropriateFlags { get; set; } 
    public int PostId { get; set; } 
    public string ControllerName { get; set; } 
    public int ReplyTo { get; set; } 
    public bool replied { get; set; } 
    public int UserProfileId { get; set; } 
} 

나는 더러운 ORM을 사용하고 있습니다. 따라서 이것은 조인을 생성하고 위에 기록 된 조인 테이블에 매핑하는 데이터 액세스 코드입니다.

public IEnumerable<CommentUserProfileJoin > GetComments(int postId, string controllerName, int replyTo) 
    { 
     IEnumerable<CommentUserProfileJoin> comments; 
     const string query = "Select c.[id], c.[CommentText], c.[Username], c.[Upvote], c.[Downvote], c.[InappropriateFlags], c.[PostId], c.[ControllerName], c.[ReplyTo], c.[replied], u.[id] as UserProfileId, u.displayname from Main.Comments c LEFT JOIN Profile.UserProfiles u on c.username = u.username where c.PostId = @postId and c.ControllerName = @contName and c.ReplyTo = @ReplyTo order by ID desc"; 
     comments = conObj.Query<CommentUserProfileJoin>(query, new { postId = postId, contName = controllerName, ReplyTo = replyTo }); 
     return comments; 
    } 

그래서 지금은 두 테이블의 조인에 대한 모델을 가지고, 나는이 당신을하는 데 도움이

@model IEnumerable<DataAccessLayer.Models.CommentUserProfileJoin> 

희망처럼 모든보기에서 사용할 수 있습니다. 또한, 의도하지 않게, 나는 모범 사례를 따를 수 없었을 것입니다. 누군가가 의견을 통해 알릴 수 있다면 기쁠 것입니다. 감사합니다.