从 ASP.NET MVC 中的不同模型调用视图

Calling a view from different models in ASP.NET MVC

在我的 ASP.NET MVC 应用程序视图中,我正在调用另一个与当前模型无关的视图。我需要一些帮助,了解如何从另一个视图调用不同的模型视图。

@model Asp_PASMVC.Models.VehicleService
@using Asp_PASMVC.Infrastructure

@{
    ViewBag.Title = "View";
    Layout = "~/Views/Shared/_Layout.cshtml";
    List<SelectListItem> CompanyList = (List<SelectListItem>)TempData.Peek("ComapnyList");
    List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
    List<SelectListItem> Employees = (List<SelectListItem>)TempData.Peek("EmployeeList");
    List<SelectListItem> Location = (List<SelectListItem>)TempData.Peek("LocationList");
    Asp_PASMVC.Models.AppRequest RequestDetails = (Asp_PASMVC.Models.AppRequest)TempData.Peek("RequestDetails");
}
@{
    Html.RenderPartial("_MainRequestView", RequestDetails);
}

@using (Html.BeginForm("WorkshopUpdate", "VehicleService", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(model => model.Req_Id)
    @Html.AntiForgeryToken()


    if (Model != null && Model.VehicleServiceApproveDetails != null)
    {
        foreach (Asp_PASMVC.Models.VehicleServiceApproveDetails Emp in Model.VehicleServiceApproveDetails)
        {
            Html.RenderPartial("_WorkshopUpdate", Emp);

        }
    }

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <!-- Default box -->
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Approver Details</h3>
                            <div class="card-tools">
                                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                                    <i class="fas fa-minus"></i>
                                </button>

                            </div>
                        </div>
                        <div class="card-body">
                            <div>
                                <fieldset id="pnlApproverList" style="display:none">
                                    <legend><h5>To whom you want to send this request for approval ? </h5> </legend>
                                    <br />
                                    <ul id="RequApprover" style="list-style-type: none">

                                        @if (Model != null && Model.ApprovalPartyList != null)
                                        {
                                            foreach (Asp_PASMVC.Models.ApprovalParty Emp in Model.ApprovalPartyList)
                                            {
                                                Html.RenderPartial("_ApprovalView", Emp);
                                            }
                                        }
                                    </ul>

                                    <button type="button" id="addAnotherApprover" class="btn btn-success" href="#" onclick="this.style.display = 'none';">Add</button>
                                    <script type="text/javascript">
                                        $(function () {
                                            // $("#movieEditor").sortable();
                                            $("#addAnotherApprover").click(function () {
                                                $.get('/VehicleService/AddApproverToReq', function (template) {
                                                    $("#RequApprover").append(template);
                                                });
                                            });
                                        });
                                    </script>
                                    <br />
                                </fieldset>

                            </div>
                        </div>

                        <!-- /.card-footer-->
                    </div>
                    <!-- /.card -->
                </div>
            </div>
        </div>

    </section>


    <div class="card-footer">
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Update and Sent" class="btn btn-success" />
            </div>
        </div>
    </div>

}

<p>
    @Html.ActionLink("Back to List", "Index")
</p>

同样这里的模型是 VehicleService。所以在那个视图中,我想调用另一个不在 vehicleservice 模型中的视图。

但我无法在此视图中加载该局部视图。有什么办法吗?

@model Asp_PASMVC.Models.ApprovalParty
@using Asp_PASMVC.Infrastructure
@{
   
    string UserLvel = TempData.Peek("UserLevelClaims").ToString();

}
<li style="padding-bottom:15px">


    @using (Html.BeginCollectionItem("ApprovalPartyList"))
    {
        <div class="row">
            <div class="col-md-5  col-sm-5">
                <div class="form-group">
                    <label>
                        @Html.RadioButtonFor(m => m.Approve_Type, false)
                        <span class="radiomargin">For Manager</span>
                    </label>
                    <br />
                    @if (UserLvel != "1")
                    {
                        <label>
                            @Html.RadioButtonFor(m => m.Approve_Type, true)
                            <span class="radiomargin">For Top Manager </span>
                        </label>
                    @Html.ValidationMessageFor(model => model.Approve_Type, "", new { @class = "text-danger" })
                    }
                </div>
            </div>
        </div>
        <br />

        <div class="row">
            <div class="col-md-6 col-sm-6">
                <div class="form-group row">
                    Select the Approver
                    <div class="col-sm-8">
                        @Html.DropDownListFor(model => model.Approver_Id, new List<SelectListItem>(), new { @id = "ddlEmployees", @class = "js-dropdown" })
                        @Html.ValidationMessageFor(model => model.Approver_Id, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
        </div>
    }
</li>

创建一个可以同时具有两个属性的 ViewModel 并将该 viewmodel 传递给 View

型号class:

public class VehicleSerivceViewModel
    {
        public VehicleService VehicleService { get; set; }

        public ApprovalParty ApprovalParty { get; set; }
    }

在视图中:

@model Asp_PASMVC.Models.VehicleServiceVewModel

将 ViewModel 传递给部分,如下所示:

@Model.ApprovalParty