ASP.NET MVC使用继承的模型为多个视图调用多个PartialView实例

时间:2017-10-24 10:39:04

标签: asp.net-mvc asp.net-mvc-4 razor

我有两个(或更多)视图,它们具有相同的基础,但略有不同,即。另外一张桌子。这将是使用PartialView删除重复代码的绝佳机会。不幸的是,在我将用于解决继承的模型的情况下是一个问题。

如何告诉PartialView,哪个View(使用哪种型号)正在调用它?

我不想在ViewBag中发送枚举,因为我不能在不改变类型的情况下使用PartialView,因为我不能在不更改类型的情况下使用PartialView,因为某些动态模型不接受某些帮助组件。

ParentFacade.cs

public class ParentFacade
{
    public Container Charts{ get; set; }
    public DashboardModel DashboardModel{ get; set; }
    public List<TimeValuePriority> ATimeValueList{ get; set; }
    public string UnitsOfVal1 { get; set; }
}

ChildFacade.cs

public class ChildFacade : ParentFacade
{
    public List<TimeMeasuredValue> BTimeValueList{ get; set; }
    public string UnitsOfVal2 { get; set; }
}

_FirstPartial.cshtml

@model  ???
@{int lenght = 0;}
        <div>
            <table class="my-table">
                <thead>
                    <tr>
                        <th colspan="2">@String.Format("{0} [{1}/{2}]", Resource.Activity, Resource.Unit, Model.UnitsOfVal1 )</th>
                    </tr>
                </thead>
                <tbody>
                    @{lenght = Model.ATimeValueList.Count; }
                    @if (lenght == 0)
                    {
                        <tr><td> </td><td> </td></tr>}
                    @for (int i = 0; i < lenght; i++)
                    {
                        if (Model.DashboardModel.Interval == IntervalEnum.Week)
                        {
                            if (i == 0)
                            {
                                <tr><td colspan="2">@Model.ATimeValueList[i].Time.ToString("d")</td></tr>
                            }
                            else
                            {
                                if (Model.ATimeValueList[i - 1].Time.Date != Model.ATimeValueList[i].Time.Date)
                                {
                                    <tr><td colspan="2">@Model.ATimeValueList[i].Time.ToString("d")</td></tr>
                                }
                            }
                        }
                        <tr>
                            <td class="number">
                                @if (Model.DashboardModel.Interval == IntervalEnum.Month)
                                {
                                    @Model.ATimeValueList[i].Time.ToString("d")
                                }
                                else
                                {
                                    @Model.ATimeValueList[i].Time.ToString("t")
                                }
                            </td>
                            <td class="number">@Model.ATimeValueList[i].Value</td>
                        </tr>}
                </tbody>
            </table>
      </div>

_SecondPartial.cshtml

@model  ???
@{int lenght = 0;}
 <div>
        <table class="My-tables">
            <thead>
                <tr>
                    @if (Model.DashboardModel.Interval != IntervalEnum.Month)
                    {
                        <th colspan="2">@String.Format("{0} [{1}]", Resource.Val2, Model.UnitsOfVal2)</th>
                    }
                    else
                    {
                        <th colspan="2">@String.Format("{0} {1} [{2}]", Resource.SumaPerDay, Resource.Val2, Model.UnitsOfVal2)</th>
                    }
                </tr>
            </thead>
            <tbody>
                @{lenght = Model.BTimeValueList.Count; }
                @if (lenght == 0)
                {
                    <tr><td> </td><td> </td></tr>}
                @for (int i = 0; i < lenght; i++)
                {

                    switch (Model.DashboardModel.Interval)
                    {
                        case IntervalEnum.Day:
                            <tr>
                                <td class="number">
                                    @Model.BTimeValueList[i].Time.ToString("t")
                                </td>
                                <td class="number">@String.Format("{0:f1}", @Model.BTimeValueList[i].Amount)</td>
                            </tr>break;
                        case IntervalEnum.Week:
                            if (i == 0)
                            {
                                <tr><td colspan="2">@Model.BTimeValueList[i].Time.ToString("d")</td></tr>
                            }
                            else
                            {
                                if (Model.BTimeValueList[i - 1].Time.Date != Model.BTimeValueList[i].Time.Date)
                                {
                                    <tr><td colspan="2">@Model.BTimeValueList[i].Time.ToString("d")</td></tr>
                                }
                            }
                            <tr>
                                <td class="number">
                                    @Model.BTimeValueList[i].Time.ToString("t")
                                </td>
                                <td class="number">@String.Format("{0:f1}", @Model.BTimeValueList[i].Amount)</td>
                            </tr>break;
                        case IntervalEnum.Month:
                            <tr><td>@Model.BTimeValueList[i].Time.ToString("d")</td><td class="number">@String.Format("{0:f1}", @Model.BTimeValueList[i].Amount)</td></tr>break;
                    }
                }
            </tbody>
        </table>
    </div>

AView.cshtml

@model Web.ControllersFacade.ParentFacade
 <div>
        @Html.HiddenFor(m => m.DashboardModel.StartDate.Date, new { @id = "startDateId" })

        <div class="row top-menu" id="top-menu">
            @Html.Partial("SharedPartials/_IntervalMenuPartial",Model.DashboardModel)
        </div>
        <div class="row row-eq-height no-margin">
            <div id="work-place" class="col-lg-10 col-md-10 col-xs-12">
                <div id="graphs" class="graphs">
                    @(Model.Charts)
                </div>
                <div>
                    <div class="btn-link left-button">
                        <span class="glyphicon glyphicon-download-alt"></span>
                        @Html.ActionLink(Resource.ExportCSV, "DownloadFile", "FileTransport", new { id = Model.DashboardModel.SelectedClientIds }, new { })
                    </div>
                    <br />
                </div>
                <div id="table">
                    @Html.Partial("SharedPartials/_FirstPartial")
                </div>
            </div>
        </div>
    </div>

BView.cshtml

@model Web.ControllersFacade.ChildFacade
 <div>
        @Html.HiddenFor(m => m.DashboardModel.StartDate.Date, new { @id = "startDateId" })

        <div class="row top-menu" id="top-menu">
            @Html.Partial("SharedPartials/_IntervalMenuPartial",Model.DashboardModel)
        </div>
        <div class="row row-eq-height no-margin">
            <div id="work-place" class="col-lg-10 col-md-10 col-xs-12">
                <div id="graphs" class="graphs">
                    @(Model.Charts)
                </div>
                <div>
                    <div class="btn-link left-button">
                        <span class="glyphicon glyphicon-download-alt"></span>
                        @Html.ActionLink(Resource.ExportCSV, "DownloadFile", "FileTransport", new { id = Model.DashboardModel.SelectedClientIds }, new { })
                    </div>
                    <br />
                </div>
                <div id="table">
                    @Html.Partial("SharedPartials/_FirstPartial")
                    @Html.Partial("SharedPartials/_SecondPartial")
                </div>
            </div>
        </div>
    </div>

0 个答案:

没有答案