[英]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.