2015-01-15 2 views
0

두 가지 모델로 작동하는보기를 만들려고합니다.잘못된 유형이보기로 전달됨 - ASP.NET MVC 5

사전에 전달 모델 항목 유형 'System.Data.Entity.DynamicProxies.ApplicationUser_D1BDF9065CBD1B8BC24F5E69ACC3CAB19A6C7FB8624B0C4435112D881B7C9CA2', 입니다하지만,이 사전의 모델 항목을 필요하지만보기에 전달 잘못된 유형에 대해 점점 오류를 유지 'StudentBookProject.ViewModel.UserPostWrapper'를 입력하십시오.

그래서 나는이 같은 두 개의 분리 된 클래스의 모든 속성에 액세스 할 수있는 간단한 래퍼 클래스 만들었습니다

public class UserPostWrapper 
{ 
    public ApplicationUser UserInfoObject { get; set; } 
    public List<Post> PostInfoObject { get; set; } 
} 

을 그리고 이것은 예외가 발생 뷰입니다 :

[Authorize] 
public ActionResult GetCurrentUser() 
{ 
    var user = UserManager.FindById(User.Identity.GetUserId()); 
    return View(user); 
} 
0 :
@model StudentBookProject.ViewModel.UserPostWrapper 

<script src="~/Scripts/jquery-1.10.2.js"></script> 
<script src="~/Scripts/jquery-ui-1.11.2.js"></script> 

@using (Html.BeginForm("Upload", "Posts", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <div class="jumbotron" style="height:300px"> 
     <div class="col-md-3"> 
      <img id="profilePhoto" src="data:image/png;base64,@Convert.ToBase64String(Model.UserInfoObject.ProfilePicture, 0, Model.UserInfoObject.ProfilePicture.Length)" width="150" /> 
     </div> 
     <div id="dialog" style="display: none;"> 
      <img src="data:image/png;base64,@Convert.ToBase64String(Model.UserInfoObject.ProfilePicture, 0, Model.UserInfoObject.ProfilePicture.Length)"> 
     </div> 
     <div class="col-md-push-5"> 
      @Html.Label(Model.UserInfoObject.Name + " " + Model.UserInfoObject.LastName, new { @class = "nameLabel" }) <br /> 
      @Html.Label(Model.UserInfoObject.DateOfBirth) <br /> 
      @Html.Label(Model.UserInfoObject.City) 
     </div> 
    </div> 

    <div class="jumbotron" style="height:170px"> 
     <div class="col-lg-7"> 

      <textarea class="expand" rows="3" cols="20" placeholder="Share something with colleagues..."></textarea> 
      <div class="icons"> 
       <a href="#" id="photoId" class="glyphicon glyphicon-camera" title="Add new photo"></a> 
       <a href="#" id="fileId" class="glyphicon glyphicon-paperclip"></a> 
       <button class="btn btn-sm btn-primary">Post</button> 
      </div> 
     </div> 
     <input type="file" id="id_default_image" style="visibility: hidden" /> 
     <input type="file" id="id_default_file" style="visibility: hidden" /> 
     <div class="col-lg-5"></div> 
    </div> 
    <br /> 
    <br /> 
    <br /> 
} 

이 보기를 렌더링하는 작업입니다

나는 무슨 일이 일어나고 있는지 잘 모르겠다. 뷰에서 어떤 모델을 명시 적으로 정의했는지 누가 잘못된 모델 유형을 뷰에 전달합니까? 내가 누락 된 내용을 아는 사람이 있습니까? 이런 종류의 접근법이 실행 가능합니까? 이것에

+0

이보기를 렌더링하는 작업을 게시하십시오. 액션의 뷰에 부정확 한 모델을 덧붙입니다. –

+0

'UserManager.FindById'는 어떤 타입을 리턴합니까? – dusky

+0

@VsevolodGoloviznin 방금 추가 한 방금 실제로 현재 사용자 정보를 반환하는 간단한 메서드입니다. –

답변

5

변경하면 작업이 :

[Authorize] 
public ActionResult GetCurrentUser() 
{ 
    var user = UserManager.FindById(User.Identity.GetUserId()); 
    var model = new UserPostWrapper { UserInfoObject = user}; 
    return View(model); 
} 

나는 코멘트에 당신이보기에 다른 모델을 전달하는 말했듯. 보기에는 UserPostWrapper 모델이 필요하지만 ApplicationUser 모델을 전달 중입니다.

+0

감사합니다. 어떤 방법이 이미 해당 뷰를 사용하고 있는지 확인하는 것을 완전히 잊어 버렸습니다 ... : - / –