【发布时间】:2017-07-19 12:04:41
【问题描述】:
问题是我无法将数据从模型传递到 PartialView。这是我的做法。
我的 PartialsController.cs
public PartialViewResult _GridView()
{
var currentUser = UserManager.FindById(User.Identity.GetUserId());
var ranks = ApplicationDbContext.Ranks.ToList();
foreach (var item in ranks)
{
if (currentUser.ReputationPoints > item.ReputationPoints && currentUser.ReputationPoints < item.PointsToAdvance)
{
currentUser.UserRankId = item.Id;
ApplicationDbContext.Entry(currentUser).State = EntityState.Modified;
ApplicationDbContext.SaveChanges();
}
}
var Rank = ApplicationDbContext.Ranks.SingleOrDefault(c => c.Id == currentUser.UserRankId);
return PartialView(currentUser);
}
而我的局部视图名为 _GridView.cshtml
@model Destiny.Models.ApplicationUser
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Zalogowano jako " + Model.CurrentUser.Gamertag, "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Wyloguj</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Zarejestruj", "Register", "Account",routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Zaloguj", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
partialView 需要另一个 ViewModel(顺便说一下,它用于我的 Home/Index),事实是我 100% 确定我在传递数据时做错了,但我不知道它是什么。这是我第一次弄乱部分视图。
【问题讨论】:
-
您看到了什么错误?你是如何渲染局部视图的?你调试过代码吗/
-
报错说控制器传入的数据是PostList(My view Model)但是view需要ApplicationUser。部分视图以这种方式呈现 @Html.Partial("~/Views/Partials/_GridView.cshtml") 它位于 _Layout.cshtml 中,我在 whatshowever 中没有传递任何模型
-
currentUser对象的类型是什么??
-
它是一个ApplicationUser
-
imgur.com/a/5ZjRe 表示传递给字典的Model是Destiny.Models.PostList的类型,同时字典需要模型Destiny.Models.ApplicationUser。正如您所看到的,我已将 ApplicationUser 传递给控制器。我怀疑控制器根本不适用于这个部分。
标签: c# asp.net-mvc-partialview