如果将其他数据加载到同一视图和部分视图中,然后呈现到选项卡中,

时间:2018-09-21 10:45:40

标签: c# jquery asp.net asp.net-mvc razor

我正在寻找一种将IF/ELSE加载到同一视图和部分视图中的不同数据的方法。

这是我的控制器:

private ViewModels.ViewWriterAmendmentsComparisonModel MapWriterAndAmendmentToViewModel(DomainModels.WriterGraph.WriterAndAmendment domainWriterAndAmendmentModel)
    {
        var writerAndActiveAmendment = new ViewWriterAmendmentsComparisonModel
        {
            OriginalWriter = new WriterAmendmentsComparisionModel
            {
                ID = domainWriterAndAmendmentModel.Original.ID,
                RowVersion = domainWriterAndAmendmentModel.Original.RowVersion,
                IsDeleted = domainWriterAndAmendmentModel.Original.IsDeleted,
                IsLocked = domainWriterAndAmendmentModel.Original.IsLocked,
                IsNew = domainWriterAndAmendmentModel.Original.IsNew,
                Version = domainWriterAndAmendmentModel.Original.Version,
                FirstName = domainWriterAndAmendmentModel.Original.FirstName,
                MiddleName = domainWriterAndAmendmentModel.Original.MiddleName,
                LastName = domainWriterAndAmendmentModel.Original.LastName,
                PRO = domainWriterAndAmendmentModel.Original.Society?.Name ?? string.Empty,
                CaeIpiNumber = domainWriterAndAmendmentModel.Original.CaeIpiNumber,
                IsProMember = domainWriterAndAmendmentModel.Original.IsProMember,

                Songs = domainWriterAndAmendmentModel.Original.WriterSplits != null
                    ? domainWriterAndAmendmentModel.Original.WriterSplits
                        .Select(x => new AssociatedSongsComparisonModel
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            SongTitle = x.RelatedWork.Title,
                            SongId = x.RelatedWork.UniqueCode,
                            CreatedDate = x.RelatedWork.CreatedDate,
                            WriterCount = GetRelatedWriterCount(x.RelatedWork.ID),
                            RecordingCount = x.RelatedWork.Recordings.Count
                        })
                        .ToList()
                    : new List<AssociatedSongsComparisonModel>(),
            },
            ActiveAmendmentWriter = new WriterAmendmentsComparisionModel
            {
                ID = domainWriterAndAmendmentModel.CurrentAmendment.ID,
                RowVersion = domainWriterAndAmendmentModel.CurrentAmendment.RowVersion,
                IsDeleted = domainWriterAndAmendmentModel.CurrentAmendment.IsDeleted,
                IsNew = domainWriterAndAmendmentModel.CurrentAmendment.IsNew,
                FirstName = domainWriterAndAmendmentModel.CurrentAmendment.FirstName,
                MiddleName = domainWriterAndAmendmentModel.CurrentAmendment.MiddleName,
                LastName = domainWriterAndAmendmentModel.CurrentAmendment.LastName,
                PRO = domainWriterAndAmendmentModel.CurrentAmendment.Society?.Name ?? string.Empty,
                CaeIpiNumber = domainWriterAndAmendmentModel.CurrentAmendment.CaeIpiNumber,
                IsProMember = domainWriterAndAmendmentModel.CurrentAmendment.IsProMember,

                Songs = domainWriterAndAmendmentModel.CurrentAmendment.WriterSplitAmendments != null
                    && domainWriterAndAmendmentModel.CurrentAmendment.WriterSplitAmendments.Any(x => x.RelatedWorkAmendment != null)
                    ? domainWriterAndAmendmentModel.CurrentAmendment.WriterSplitAmendments
                        .Select(x => new AssociatedSongsComparisonModel
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            SongId = x.RelatedWorkAmendment.UniqueCode,
                            SongTitle = x.RelatedWorkAmendment.Title,
                            CreatedDate = x.RelatedWorkAmendment.CreatedDate,
                            WriterCount = GetRelatedWriterAmendmentCount(x.RelatedWorkAmendment.ID),
                            RecordingCount = x.RelatedWorkAmendment.RecordingAmendments.Count
                        })
                        .ToList()
                    : new List<AssociatedSongsComparisonModel>(),
            }
        };
        return writerAndActiveAmendment;
    }

    private int GetRelatedWriterCount(Guid id)
    {
        var count = Task.Run(async () =>  await _artistAccountService.GetOriginalWorkWritersCountAsync(id));
        return count.Result;
    }

    private int GetRelatedWriterAmendmentCount(Guid id)
    {
        var count = Task.Run(async () => await _artistAccountService.GetWorkAmendmentWriterAmendmentCountAsync(id));
        return count.Result;
    }

    [HttpGet]
    [ActionName("ViewWriterAmendments")]
    [Route("{writercode}/ViewWriterAmendments")]
    [AuthorizeTenancy(Roles = "super,administrator")]
    public async Task<ActionResult>ViewWriterAmendments(string writercode, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage, string sortBy = "Name", bool sortAsc = true)
    {
        if (Request.QueryString["IsAmendment"] != null)
        {
            ViewBag.IsAmendment = Request.QueryString["IsAmendment"];
        }
        else
        {
            ViewBag.IsAmendment = "0";
        }

        // TEST IF VALID
        // ajax method - refuse
        if (Request.IsAjaxRequest())
        {
            return RedirectToAction("WriterAmendments", "SongsManagement");
        }

        // no writer - refuse
        if (string.IsNullOrEmpty(writercode))
        {
            return RedirectToAction("WriterAmendments", "SongsManagement", new { page, take, sortBy, sortAsc });
        }

        // get originalWithPossibleActiveAmendment work
        var writerAndAmendment = await _artistAccountService.GetWriterAndAmendmentGraphAsync(writercode, includeWritersSplitsAndWorks: true, includeWriterSociety: true);
        // not found? - refuse
        if (writerAndAmendment.Original == default(WriterGraph.Writer))
        {
            return RedirectToAction("WriterAmendments", "SongsManagement", new { page, take, sortBy, sortAsc });     // something wrong
        }
        // writer locked? - refuse
        if (writerAndAmendment.Original.IsLocked)
        {
            return RedirectToAction("WriterAmendments", "SongsManagement", new { page, take, sortBy, sortAsc });     // something wrong
        }

        // setup view model
        var viewModel = MapWriterAndAmendmentToViewModel(writerAndAmendment);

        // return view
        return View(viewModel);
    }
}

}

这是我尝试过的部分视图:,但是我不确定它是否是最佳方法:

<div class="panel">
<div class="panel-heading">
    <span class="panel-title">
        <i class="glyphicon glyphicon-music"></i>&nbsp; Writer Details
    </span>
</div>

<fieldset>
    <legend style="display: none">Review Song Details Form</legend>
    @Html.AntiForgeryToken()

    @* artist account hidden fields *@
    @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
    {
        @Html.HiddenFor(m => m.ActiveAmendmentWriter.ID)
        @Html.HiddenFor(m => m.ActiveAmendmentWriter.RowVersion)
        @Html.HiddenFor(m => m.ActiveAmendmentWriter.IsDeleted)
    }
    else
    {
        @Html.HiddenFor(m => m.OriginalWriter.ID)
        @Html.HiddenFor(m => m.OriginalWriter.RowVersion)
        @Html.HiddenFor(m => m.OriginalWriter.IsDeleted)
    }


    @* editable fields *@
    <div class="panel-body p25 fill bt0">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div id="reviewDetailsErrorSummary" class="text-danger wizardErrorSummary" style="display: none;">
            Please correct the following errors:
            <ul>
                <li></li>
            </ul>
        </div>

        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* First Name *@
            <div class="form-group">
                <label class="control-label col-md-4">First Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.ActiveAmendmentWriter.FirstName, new { @class = "form-control-static text-muted", data_bind = "value: FirstName" })
                </div>
            </div>
        }
        else
        {
            @* First Name *@
            <div class="form-group">
                <label class="control-label col-md-4">First Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.OriginalWriter.FirstName, new { @class = "form-control-static text-muted", data_bind = "value: FirstName" })
                </div>
            </div>
        }

        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* Middle Name *@
            <div class="form-group">
                <label class="control-label col-md-4">Middle Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.ActiveAmendmentWriter.MiddleName, new { @class = "form-control-static text-muted", data_bind = "value: MiddleName" })
                </div>
            </div>
        }
        else
        {
            @* Middle Name *@
            <div class="form-group">
                <label class="control-label col-md-4">Middle Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.OriginalWriter.MiddleName, new { @class = "form-control-static text-muted", data_bind = "value: MiddleName" })
                </div>
            </div>
        }

        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* Last Name *@
            <div class="form-group">
                <label class="control-label col-md-4">Last Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.ActiveAmendmentWriter.LastName, new { @class = "form-control-static text-muted", data_bind = "value: LastName" })
                </div>
            </div>
        }
        else
        {
            @* Last Name *@
            <div class="form-group">
                <label class="control-label col-md-4">Last Name</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.OriginalWriter.LastName, new { @class = "form-control-static text-muted", data_bind = "value: LastName" })
                </div>
            </div>
        }

        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* Is the Writer a member of a Performing Rights Organisation (PRO)? *@
            <div class="form-group">
                <label class="control-label col-md-4">Is the writer a member of a Performing Rights Organisation (PRO)?</label>
                <div class="col-md-8">
                    <div class="theme-primary form-control-static text-muted">
                        @if (Model.ActiveAmendmentWriter.IsProMember == true)
                        {
                            <span>Yes</span>
                        }
                        else
                        {
                            <span>No</span>
                        }
                    </div>
                </div>
            </div>
        }
        else
        {
            @* Is the Writer a member of a Performing Rights Organisation (PRO)? *@
            <div class="form-group">
                <label class="control-label col-md-4">Is the writer a member of a Performing Rights Organisation (PRO)?</label>
                <div class="col-md-8">
                    <div class="theme-primary form-control-static text-muted">
                        @if (Model.OriginalWriter.IsProMember == true)
                        {
                            <span>Yes</span>
                        }
                        else
                        {
                            <span>No</span>
                        }
                    </div>
                </div>
            </div>
        }
        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* Society Membership *@
            <div class="form-group">
                <label class="control-label col-md-4">Please select which PRO the writer is a member of</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.ActiveAmendmentWriter.SocietyID, new { @class = "form-control-static text-muted", data_bind = "value: CaeIpiNumber" })
                </div>
            </div>
        }
        else
        {
            @* Society Membership *@
            <div class="form-group">
                <label class="control-label col-md-4">Please select which PRO the writer is a member of</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.OriginalWriter.SocietyID, new { @class = "form-control-static text-muted", data_bind = "value: CaeIpiNumber" })
                </div>
            </div>
        }
        @if (ViewBag.IsAmendment == "1" && !Model.OriginalWriter.IsNew)
        {
            @* CAEIPI Number *@
            <div class="form-group">
                <label class="control-label col-md-4">CAE/IPI Number</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.ActiveAmendmentWriter.CaeIpiNumber, new { @class = "form-control-static text-muted", data_bind = "value: CaeIpiNumber" })
                </div>
            </div>
        }
        else
        {
            @* CAEIPI Number *@
            <div class="form-group">
                <label class="control-label col-md-4">CAE/IPI Number</label>
                <div class="col-md-4 form-control-static text-muted">
                    @Html.DisplayFor(m => m.OriginalWriter.CaeIpiNumber, new { @class = "form-control-static text-muted", data_bind = "value: CaeIpiNumber" })
                </div>
            </div>
        }
</fieldset>

标签

<div class="page-tabs">
<ul class="nav nav-tabs">
    @if (!Model.OriginalWriter.IsNew)
    {

        <li class='@(ViewBag.IsAmendment == "0" ? "active" : "")'>
            <a href='@Url.Action("ViewWriterAmendments", "SongsManagement", new { isAmendment = 0})' title="Original">Original</a>
        </li>
        <li class='@(ViewBag.IsAmendment == "1" ? "active" : "")'>
            <a href='@Url.Action("ViewWriterAmendments", "SongsManagement", new { isAmendment = 1 })' title="Amendment">Active Amendment</a>
        </li>
    }
</ul>

表格

<div class="panel-body pn">
    <table class="table table-striped table-hover dataTable admin-form theme-primary viewwriteramendmentsTable" cellspacing="0" width="100%" role="grid">
        <thead id="tableHeader">
            <tr>

                <th class="text-left bg-white" data-container="body" data-toggle="tooltip" title="Song Id">Song ID</th>
                <th class="text-left bg-white" data-container="body" data-toggle="tooltip" title="Song Title">Song Title</th>
                <th class="hidden-xs hidden-sm text-left bg-white" data-container="body" data-toggle="tooltip" title="Created Date">Created Date</th>
                <th class="text-center bg-white" data-container="body" data-toggle="tooltip" title="Writers">Writers</th>
                <th class="text-center bg-white" data-container="body" data-toggle="tooltip" title="Share">Recordings</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var t in Model.OriginalWriter.Songs)
            {
                <tr data-id="@t.ID"
                    data-isdeleted="@t.IsDeleted"
                    data-rowversion="@t.RowVersion"
                    data-writer-songid="@t.SongId"
                    data-writer-songtitle="@t.SongTitle"
                    data-writer-createddate="@t.CreatedDate"
                    data-writercount="@t.WriterCount"
                    data-recordingcount="@t.RecordingCount">

                    <td>@t.SongId</td>
                    <td class="text-left">@t.SongTitle</td>
                    <td class="hidden-xs hidden-sm">@t.CreatedDate.ToString(string.Format("dd/MM/yyyy"))</td>
                    <td><span class="label label-rounded label-info">@t.WriterCount</span></td>
                    <td><span class="label label-rounded label-info">@t.RecordingCount</span></td>
                </tr>
            }
        </tbody>
    </table>

我基本上是在寻找一种可以拥有相同视图的方法,但是当您单击选项卡时,它会根据控制器中的逻辑呈现“原始”或“修订”。任何建议都很好。

0 个答案:

没有答案