Home > Software engineering >  Calling a view from different models in ASP.NET MVC
Calling a view from different models in ASP.NET MVC

Time:10-16

In my ASP.NET MVC application in the view, I'm calling another view that is not related to the current model. There I need some help that how to call the different model views from another view.

enter image description here

@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>

So likewise here the model is VehicleService. So within that view, I want to call another view that is not within the vehicleservice model.

But I cannot load that partial view within this view. Is there any way to do this?

enter image description here

@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>

CodePudding user response:

Create a ViewModel which can have both Properties and pass that viewmodel to View

Model class:

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

        public ApprovalParty ApprovalParty { get; set; }
    }

In View :

@model Asp_PASMVC.Models.VehicleServiceVewModel

pass ViewModel to partial as below:

@Model.ApprovalParty
  • Related