nem*_*_87 0 c# asp.net-mvc razor asp.net-mvc-5
我正在尝试创建适用于两种不同模型的视图.但是我一直在查看视图中传递的错误类型的错误:
传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies.ApplicationUser_D1BDF9065CBA1B8BC24F5E69ACC3CAB19A6C7FB8624B0C4435112D881B7C9CA2',但此字典需要类型为'StudentBookProject.ViewModel.UserPostWrapper'的模型项.
我已经创建了一个简单的包装类,所以我可以访问两个分离的类的所有属性,如下所示:
public class UserPostWrapper
{
public ApplicationUser UserInfoObject { get; set; }
public List<Post> PostInfoObject { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是抛出异常的视图:
@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 />
}
Run Code Online (Sandbox Code Playgroud)
这是呈现视图的操作:
[Authorize]
public ActionResult GetCurrentUser()
{
var user = UserManager.FindById(User.Identity.GetUserId());
return View(user);
}
Run Code Online (Sandbox Code Playgroud)
我不确定我明白发生了什么.谁将错误的模型类型传递给视图,如果我已明确定义了我在该视图中使用的模型?有谁知道我在这里缺少什么?这种方法是否可行?
将您的操作更改为:
[Authorize]
public ActionResult GetCurrentUser()
{
var user = UserManager.FindById(User.Identity.GetUserId());
var model = new UserPostWrapper { UserInfoObject = user};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
正如我在评论中所说,你将一个不同的模型传递给你的观点.视图需要UserPostWrapper模型,但您传递的是ApplicationUser模型.
| 归档时间: |
|
| 查看次数: |
549 次 |
| 最近记录: |