Pregunta

I read some of the questions on creating a view that contains multiple repositories but I can't seem to get the correct combination to get it to work.

I 3 repositories that connect to three different tables in a SQL database. I have controllers and viewmodels for each that display the information correctly. I can't seem to get partial views to work. When I create a view and than try to use the partialview in the view I receive error that the model does not contain the repository. I am wondering if I need to have only one file with 3 repository classes in it.

I have: DomainPlayerRepository.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Games;

namespace Domain.Abstract
{
    public interface DomainPlayerRepository
    {
        IQueryable<Player> Player { get; }
    }

}

DomainGameRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Games;

namespace Domain.Abstract
{
    public interface DomainGameRepository
    {
        IQueryable<Game> Game { get; }
    }
}

GamePartialView.cshtml

@model Domain.Games.Game

<div class="Game">
    @Model.Date   @Model.NumPlayers @Model.PotMoney

</div>

List.cshtml (for Player)

@model IEnumerable<Domain.Games.Player>

@{
    ViewBag.Title = "Players";
}

<h2>Player</h2>

@foreach (var p in Model) 
{
    <div class ="player">
       <h3>@p.FName  @p.LName Handicap @p.Handicap.ToString()</h3>
    </div>

}

@foreach (var p in Model)
{
    <div class ="game">
      @Html.Partial("GamePartialView", p)
    </div>

}

I have also been trying to get a viewmodel with both my repositories in it but that is not working yet.

MultiRepositoryViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Domain.Games;

namespace WebUI.Models
{
    public class MultiRepositoryViewModel
    {
        public IEnumerable<Player> Player { get; set; }
        public IEnumerable<Game> Game { get; set; }
    }
}
¿Fue útil?

Solución

Change the model of the view to the ViewModel and use the properties you've defined

@model WebUI.Models.MultiRepositoryViewModel

@{
    ViewBag.Title = "Players";
}

<h2>Player</h2>

@foreach (var p in Model.Player) 
{
    <div class ="player">
       <h3>@p.FName  @p.LName Handicap @p.Handicap.ToString()</h3>
    </div>
}

@foreach (var p in Model.Game)
{
    <div class ="game">
       @Html.Partial("GamePartialView", p)
    </div>
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top