如何将模型的一部分传递到局部视图

时间:2018-09-19 15:05:43

标签: c# view viewmodel partial

在这里,我有一个视图模型,该模型将数据填充到视图中,我希望能够分离出这部分代码:

MappingMode="Absolute"

,并将属性设置为相关的视图模型,即WinnersAuthorVM和WinnersContributorVM
就像这样,这可能是错误的,但是试图实现...

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,

现在我想将此传递给局部视图。_AwardWinnerCard我可以做,但是在局部视图中时 我尝试像@ Model.Authors一样访问。...我没有得到属性?我在这里想念什么?

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]
})

查看模型:

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>

0 个答案:

没有答案