无法将数据从模型传递到局部视图
Can't pass data from model into Partial View
问题是我无法将数据从模型传递到 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% 确定我在传递数据时做错了什么,但我不知道它是什么。这是我第一次弄乱部分视图。
使用
@Html.RenderAction
而不是
@Html.Partial( in the layout –
这边
@{Html.RenderAction("_GridView");}
问题是我无法将数据从模型传递到 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% 确定我在传递数据时做错了什么,但我不知道它是什么。这是我第一次弄乱部分视图。
使用
@Html.RenderAction
而不是
@Html.Partial( in the layout –
这边
@{Html.RenderAction("_GridView");}