Reputation: 344
The Issue is I can't pass data from model to PartialView. Here is how I've done it.
My 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);
}
And my Partial View called _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>
}
The partialView demands another ViewModel (Which is used for my Home/Index by the way), and the thing is the fact I am 100% sure that I've done something wrong with passing data, but I don't know what is it. It's my 1st time messing with partial Views.
Upvotes: 1
Views: 423
Reputation: 967
use
@Html.RenderAction
instead of
@Html.Partial( in the layout –
This Way
@{Html.RenderAction("_GridView");}
Upvotes: 2