Как передать часть модели в частичный вид - PullRequest
0 голосов
/ 19 сентября 2018

Здесь у меня есть модель представления, которая заполняет данные для представления, я хочу иметь возможность отделить эту часть кода:

Contact = x.Type == ContentTypes.Author
    ? authors.TryGetValue(x.WinnerId, out var author) ? author.Name : "Unknown author"
    : allContribDictionary.TryGetValue(x.WinnerId, out var name) ? name : Firm.UnknownFirmDescription,
Url = x.Type == ContentTypes.Author
    ? authors.TryGetValue(x.WinnerId, out var authorProfile) ? authorProfile.URL : "#"
    : contributors[x.WinnerId],
ImageUrl = x.Type == ContentTypes.Author
    ? $"/images/authors/get.ashx?w=75&h=100&g={authorService.GetByRef(x.WinnerId)?.AuthorGuid}"
    : contributorService.GetContributor(x.WinnerId, true)?.LogoImageUrlLocal,

и установить свойства для соответствующей модели представления, т.е. WinnersAuthorVM иWinnersContributorVM
вроде как так, что может быть неправильно, но пытаться достичь ...

Authors = authorService.GetByRef(authors.Select(a => a.Key)).Select(wa => new WinnersAuthorVM
{
    AuthorName = authors.TryGetValue(x.WinnerId, out var author) ? author.Name : "Unknown author",
    AuthorURL = author.URL ??  "#"
}),
Contributors = contributorService.GetContributor(contributors.Select(a => a.Key)).Select(wf => new WinnersContributorVM
{
    FirmName = allContribDictionary.TryGetValue(x.WinnerId, out var name) ? name : Firm.UnknownFirmDescription,
    FirmLogoURL = contributorService.GetContributor(x.WinnerId, true)?.LogoImageUrlLocal,
    FirmURL = contributors[x.WinnerId]
})

Теперь я хочу передать это в частичное представление .. _AwardWinnerCard, что я могу сделать, но в частичном представлении, когдаЯ пытаюсь получить доступ как @ Model.Authors .... Я не получаю свойства?Что мне здесь не хватает?

var viewModel = new AwardWinnersWrapperVM
{
    SummaryText = websiteTextService.GetContent("LEX_AWARDWINNERS_LANDING", "EN"),
    SelectedJurisdiction = additionalJurisdictionSearch,
    SelectedWorkarea = additionalWorkareaSearch,
    Pagination = new WinnersPagination
    {
        WinnersPerPage = winnersPerPage,
        Page = pageNumber,
        PageNoQueryStringKey = nameof(pageNumber),
        BeginAmount = 2,
        MiddleAmount = 3,
        EndAmount = 2,
    },
    AwardWinners = awardWinners.Select(x => new AwardWinnersViewModel
    {
        Type = x.Type,
        SeriesName = x.AwardName,
        Jurisdiction = x.Jurisdiction,
        JurisdictionRef = x.JurisdictionRef,
        WorkareaRef = x.WorkareaRef,
        Workarea = x.Workarea,
        Contact = x.Type == ContentTypes.Author
            ? authors.TryGetValue(x.WinnerId, out var author) ? author.Name : "Unknown author"
            : allContribDictionary.TryGetValue(x.WinnerId, out var name) ? name : Firm.UnknownFirmDescription,
        Url = x.Type == ContentTypes.Author
            ? authors.TryGetValue(x.WinnerId, out var authorProfile) ? authorProfile.URL : "#"
            : contributors[x.WinnerId],
        ImageUrl = x.Type == ContentTypes.Author
            ? $"/images/authors/get.ashx?w=75&h=100&g={authorService.GetByRef(x.WinnerId)?.AuthorGuid}"
            : contributorService.GetContributor(x.WinnerId, true)?.LogoImageUrlLocal,
    })
    .Where(x => (additionalJurisdictionSearch == -1 || x.JurisdictionRef == additionalJurisdictionSearch) && (additionalWorkareaSearch == -1 || x.WorkareaRef == additionalWorkareaSearch))
    .Skip(winnersPerPage * (pageNumber - 1))
    .Take(winnersPerPage)
};

Просмотр моделей:

public class AwardWinnersWrapperVM
{
    public IEnumerable<WinnersFilterSearch> JurisdictionFilter { get; set; }
    public IEnumerable<WinnersFilterSearch> WorkareaFilter { get; set; }
    public IEnumerable<AwardWinnersViewModel> AwardWinners { get; set; }
    public WinnersPagination Pagination { get; set; }
    public WebsiteContent SummaryText { get; set; }
    public int? SelectedJurisdiction { get; set; }
    public int? SelectedWorkarea { get; set; }
}

public class AwardWinnersViewModel
{
    public IEnumerable<WinnersAuthorVM> Authors { get; set; }
    public IEnumerable<WinnersContributorVM> Firms { get; set; }
    public ContentTypes Type { get; set; }
    public Guid? AuthorGuid { get; set; }
    public Guid? ContributorGuid { get; set; }
    public string SeriesName { get; set; }
    public int JurisdictionRef { get; set; }
    public int WorkareaRef { get; set; }
    public string Jurisdiction { get; set; }
    public string Workarea { get; set; }
    public string Contact { get; set; }
    public string ImageUrl { get; set; }
    public string Url { get; set; }
}

public class WinnersFilterSearch
{
    public int JurisdictionRef { get; set; }
    public int WorkareaRef { get; set; }
    public string Jurisdiction { get; set; }
    public string Workarea { get; set; }
}

public class WinnersAuthorVM
{
    public Guid AuthorGuid { get; set; }
    public string AuthorName { get; set; }
    public string AuthorPhotoURL { get; set; }
    public string AuthorURL => $"/images/authors/get.ashx?w=75&h=100&g={this.AuthorGuid}";
}

public class WinnersContributorVM
{
    public string FirmName { get; set; }
    public string FirmLogoURL { get; set; }
    public string FirmURL { get; set; }
}

public class WinnersPagination : IPaginationSettings
{
    public int Page { get; set; }
    public int? ResultsOutOf { get => (this.WinnersPerPage * this.Page ) < this.TotalRows ? (this.WinnersPerPage * this.Page) : this.TotalRows; }
    public int TotalRows { get; set; }
    public int WinnersPerPage { get; set; }
    public int TotalPages { get => this.TotalRows / WinnersPerPage; set { } }
    public int BeginAmount { get; set; }
    public int MiddleAmount { get; set; }
    public int EndAmount { get; set; }
    public string PageNoQueryStringKey { get; set; }
}

Основной вид (код детали)

<div class="leftSection">
    <div class="col-xs-12 col-sm-6 col-md-6">
        @foreach (var contributor in Model.AwardWinners.Where(x => x.Type == Enums.ContentTypes.Contributor))
        {

            @Html.Partial("_AwardWinnerCard", contributor)
        }
    </div>
    <div class="col-xs-12 col-sm-6 col-md-6">
        @foreach (var author in Model.AwardWinners.Where(x => x.Type == Enums.ContentTypes.Author))
        {
            @Html.Partial("_AwardWinnerCard", author)
        }
    </div>
</div>

Частичный вид

<div class="colourbar"></div>
<div class="awardWinnersBox">
    <div class="top-info">
        <div class="@(Model.Type == Enums.ContentTypes.Author ? "authorPhoto" : "firmLogo")">
            <img src="@Model.ImageUrl" alt="" onerror="this.src = '/images/noProfilePhoto.png'" />
        </div>

        <div class="verticalDivide"></div>
        <div class="award">
            @if (Model.Type == Enums.ContentTypes.Author)
            {
                <img alt="Thought Leader" class="typeImage" src="@Url.Content("~/images/AwardWinners/Legal-influencer-logo.png")" />
            }
            else
            {
                <img alt="Legal Influencer" class="typeImage" src="@Url.Content("~/images/AwardWinners/thought-leader-logo.png")" />
            }
            <div class="awardName">
                @Model.SeriesName
            </div>
        </div>
    </div>

    <div class="title">
        <a href="@Model.Url" target="_blank">@Model.Contact</a>
    </div>
    <div class="description">
        @Model.Workarea
        <br />
        @Model.Jurisdiction
    </div>
</div>
...