Reputation: 4781
I'm trying to create view that works with two different models. But I keep getting error about wrong type passed in view:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.ApplicationUser_D1BDF9065CBD1B8BC24F5E69ACC3CAB19A6C7FB8624B0C4435112D881B7C9CA2', but this dictionary requires a model item of type 'StudentBookProject.ViewModel.UserPostWrapper'.
I've created a simple wrapper class to so I can access all the properties of two separated classes like this:
public class UserPostWrapper
{
public ApplicationUser UserInfoObject { get; set; }
public List<Post> PostInfoObject { get; set; }
}
And this is the view where the exception is thrown:
@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 />
}
This is the action that renders View:
[Authorize]
public ActionResult GetCurrentUser()
{
var user = UserManager.FindById(User.Identity.GetUserId());
return View(user);
}
I'm not sure I understand what is happening. Who is passing wrong model type to an view, if I've explicitly defined which model I'm using in that view? Does anyone knows what I'm missing here? And is this kind of an approach workable?
Upvotes: 0
Views: 722
Reputation: 12324
Change you action to this:
[Authorize]
public ActionResult GetCurrentUser()
{
var user = UserManager.FindById(User.Identity.GetUserId());
var model = new UserPostWrapper { UserInfoObject = user};
return View(model);
}
As I've said in the comments you're passing a different model into your view. The view expects the UserPostWrapper
model but you're passing the ApplicationUser
model.
Upvotes: 5