简体   繁体   中英

Get Object from PageModel in Partial View

I have Companies object in the PageModel, it is as a dropdown in the cshtml, and I have a Partial View for the Information table, the table is depending on the selected company of the dropdown list. Every user has roll and permissions for every Company and based on it users have permissions for the information (eg Edit the Information, Delete the Information). I want to get the selected Company and get from these permissions as dropdowns in the information table.

The main CSHTML

@page
@model Fachinformationsdienst_Kundenportal.Pages.Information_listModel
@{
}
<div class="form-group col-md-4">
    <label for="inputState">Unternehmen</label>
    <select id="inputState" class="form-control">
        <option selected>Wählen Sie die Firma aus...</option>
        @for (int i = 0; i < Model.companies.Count; i++)
        {
            <option>@Model.companies[i].FirmenKurzBezeichnung</option>
        }
    </select>
</div>
<div id="fachinfoContainer">
    <partial name="_FachinfoPartial" model="@Model.fachinfos" />
</div>

@section Scripts{
    <script type="text/javascript">
        $(function () {
            $("#inputState").change(function () {
                var selectcompany = "";
                if ($(this).val() != "Wählen Sie die Firma aus...") {
                    selectcompany = $(this).val();
                }
                $.ajax({
                    url: "/Actions/Information-List?handler=fachinfoPartial",
                    type: "Get",
                    data: { company: selectcompany },
                    success: function (result) {
                        $("#fachinfoContainer").html(""); //clear the fachinfo container.
                        $("#fachinfoContainer").html(result); //populate the container.
                    },
                    error: function (result) {
                        alert(result);
                    }
                });
            });
        });
    </script>
}

Partial View

@model List<Fachinformationsdienst_Kundenportal.Models.Fachinfo>

<table class="table table-striped" id="FachinfoTable">
    <thead>
        <tr>
            <th scope="col">Nr.</th>
            <th scope="col">Name</th>
            <th scope="col">Status</th>
            <th scope="col">Letzte Änderung</th>
            <th scope="col">Aktuelle Version</th>
            <th scope="col">Auftrag</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <th scope="row">@Model[i].FachinfoNummer</th>
                <td>@Model[i].FachinfoName</td>
                <td>@Model[i].Status</td>
                <td>@Model[i].Datum</td>
                <td>@Model[i].PdfVersion</td>
                <td>
                    <div class="btn-group">
                        <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
                            <span class="caret"></span>
                            <span class="sr-only">Toggle Dropdown</span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            @for (int c = 0; c < company.permission.count; c++)
                            {
                                <li><a href="#">company.permission[c]</a></li>
                            }
                        </ul>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>

The PageModel

using Fachinformationsdienst_Kundenportal.Classes;
using Fachinformationsdienst_Kundenportal.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace Fachinformationsdienst_Kundenportal.Pages
{
    public class Information_listModel : PageModel
    {
        public List<Company> companies { get; set; }
        public List<Fachinfo> fachinfos = new List<Fachinfo>();

        public void OnGet()
        {
            companies = APIRequester.GetCompanies(User.Identity.Name);
            foreach (var company in companies)
            {
                fachinfos.AddRange(APIRequester.GetFachinfos(company.FirmenKurzBezeichnung));
            }
        }
        public PartialViewResult OnGetFachinfoPartial(string company)
        {
            //based on the selctedcompany to filter data, then return to the partial view.
            fachinfos = APIRequester.GetFachinfos(company);
            return Partial("_FachinfoPartial", fachinfos);
        }

    }
}

The Copmany class

public class Company
{
public enum Permission
        {
            ERSTERFASSEN,
            AENDERN,
            FREIGEBEN,
            SPERREN,
            LOESCHEN,
            ABFRAGEN,
            DRUCKEN
        }

        public string FirmenKurzBezeichnung { get; set; }
        public string Rolle { get; set; }
        public Permission permission { get; set; }
}

You may define a ViewModel that contains the company and fachinfos, and then use this viewmodel as the pagemodel of the partialview.

public class MyViewModel
{
    public Company SelectedCompany { get; set; }
    public List<Fachinfo> Fachinfos { get; set; }
}

Return this model to the partial View:

public PartialViewResult OnGetFachinfoPartial(string company)
{
    //based on the selctedcompany to filter data, then return to the partial view.
    var myViewModel = new MyViewModel()
    {
        SelectedCompany = GetCompany(company),     //get the company object here
        Fachinfos = APIRequester.GetFachinfos(company)
    };

    return Partial("_FachinfoPartial", myViewModel);
}

And in partialview:

@model Namespace.MyViewModel

Then you can get Company and Fachinfo in view like below:

Model.SelectedCompany 
Model.Fachinfos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM