在分部视图中从 PageModel 获取对象
Get Object from PageModel in Partial View
我在 PageModel 中有公司对象,它是 cshtml 中的下拉列表,我有信息的部分视图 table,table 取决于所选的公司下拉列表。
每个用户都拥有每个公司的角色和权限,并且基于它,用户对信息具有权限(例如,编辑信息、删除信息)。
我想获取选定的公司并从这些权限中获取信息 table.
中的下拉列表
主要的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>
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);
}
}
}
公司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作为partialview的pagemodel。
public class MyViewModel
{
public Company SelectedCompany { get; set; }
public List<Fachinfo> Fachinfos { get; set; }
}
Return此模型到局部视图:
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
我在 PageModel 中有公司对象,它是 cshtml 中的下拉列表,我有信息的部分视图 table,table 取决于所选的公司下拉列表。 每个用户都拥有每个公司的角色和权限,并且基于它,用户对信息具有权限(例如,编辑信息、删除信息)。 我想获取选定的公司并从这些权限中获取信息 table.
中的下拉列表主要的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>
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);
}
}
}
公司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作为partialview的pagemodel。
public class MyViewModel
{
public Company SelectedCompany { get; set; }
public List<Fachinfo> Fachinfos { get; set; }
}
Return此模型到局部视图:
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