繁体   English   中英

从局部视图中的 PageModel 获取 Object

[英]Get Object from PageModel in Partial View

我在 PageModel 中有公司 object,它是 cshtml 中的下拉列表,我有一个信息表的部分视图,该表取决于下拉列表中选择的公司。 每个用户对每个公司都有滚动和权限,并且基于它,用户对信息具有权限(例如,编辑信息、删除信息)。 我想获取选定的公司并从这些权限中获取信息表中的下拉菜单。

主要的 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>
}

局部视图

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

页面模型

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);
        }

    }
}

公司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; }
}

您可以定义一个包含公司和 fachinfos 的 ViewModel,然后将此 viewmodel 用作部分视图的页面模型。

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

将此 model 返回到局部视图:

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);
}

并在局部视图中:

@model Namespace.MyViewModel

然后您可以在如下视图中获取 Company 和 Fachinfo:

Model.SelectedCompany 
Model.Fachinfos

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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