【问题标题】:Get Object from PageModel in Partial View在局部视图中从 PageModel 获取对象
【发布时间】:2021-05-19 08:27:16
【问题描述】:

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

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

    }
}

公司类

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

【问题讨论】:

    标签: javascript c# html asp.net-core razor-pages


    【解决方案1】:

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

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

    将此模型返回到局部视图:

    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
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多