1

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.

13
  • What error you are seeing? How are you rendering the partial view? Did you debug the code/ Commented Jul 19, 2017 at 12:07
  • The error said that the data passed in the controller is PostList (My view Model) but view demands ApplicationUser. Partial view was rendered this way @Html.Partial("~/Views/Partials/_GridView.cshtml") And it is in _Layout.cshtml which i have passed no model in whatshowever Commented Jul 19, 2017 at 12:10
  • what is the type of currentUser object?? Commented Jul 19, 2017 at 12:10
  • It is an ApplicationUser Commented Jul 19, 2017 at 12:11
  • imgur.com/a/5ZjRe It says that Model passed to dictionary is type of Destiny.Models.PostList, meanwhile dictionary demands model Destiny.Models.ApplicationUser. As you can see though, I've passed ApplicationUser to the controller. I suspect that controller doesn't work with this partial at all. Commented Jul 19, 2017 at 12:15

1 Answer 1

2

use

   @Html.RenderAction 

instead of

   @Html.Partial( in the layout –

This Way

 @{Html.RenderAction("_GridView");}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.