在部分视图中获取对象
Get object inside partial View
朋友,
我有一个名为 "OtherDatas.cshtml" 的视图页面,我需要在她内部呈现一个 PartialView。因此,为此,我需要检查 partialView 中的对象是否为 null。如果没有,partialView 就会出现。如何获取 partialView 中的对象?这是我的代码:
OtherDatas.cshtml:
<div class="table-responsive">
@{
if (//I need to check the object inside ){
Html.Partial("~/Views/Outrosdados/Search.cshtml");
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
局部视图Search.html:
@model TB_DADOS_MANUAIS
@using EixoX.Html;
@using Gestão.Models;
@using System.Globalization;
@{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
@foreach (TB_OUTROS_DADOS dados in od){
<tr>
<td>@dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />@dados.VL_AMORTIZACAO</td>
<td><span class="money" />@dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />@dados.VL_ATRASO</td>
<td><span class="money" />@dados.VL_DAMP3</td>
</tr>
}
</table>
如果我在 'od' 列表中有一些 'TB_OUTROS_DADOS',我需要检查 'foreach' 的内部。
谢谢!
你不能以这种方式做到这一点。
将 List<> 作为模型传递给您的父视图,或者以与在局部视图中相同的方式声明此列表,然后进行检查,如果合适,则将其传递给局部视图。
这样:
@{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
<div class="table-responsive">
@{
if (od.Count > 0){
Html.Partial("~/Views/Outrosdados/Search.cshtml",od);
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
并且在 PartialView 中
@model List<TB_OUTROS_DADOS>
@using EixoX.Html;
@using Gestão.Models;
@using System.Globalization;
@{
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
@foreach (TB_OUTROS_DADOS dados in Model){
<tr>
<td>@dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />@dados.VL_AMORTIZACAO</td>
<td><span class="money" />@dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />@dados.VL_ATRASO</td>
<td><span class="money" />@dados.VL_DAMP3</td>
</tr>
}
</table>
朋友,
我有一个名为 "OtherDatas.cshtml" 的视图页面,我需要在她内部呈现一个 PartialView。因此,为此,我需要检查 partialView 中的对象是否为 null。如果没有,partialView 就会出现。如何获取 partialView 中的对象?这是我的代码:
OtherDatas.cshtml:
<div class="table-responsive">
@{
if (//I need to check the object inside ){
Html.Partial("~/Views/Outrosdados/Search.cshtml");
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
局部视图Search.html:
@model TB_DADOS_MANUAIS
@using EixoX.Html;
@using Gestão.Models;
@using System.Globalization;
@{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
@foreach (TB_OUTROS_DADOS dados in od){
<tr>
<td>@dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />@dados.VL_AMORTIZACAO</td>
<td><span class="money" />@dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />@dados.VL_ATRASO</td>
<td><span class="money" />@dados.VL_DAMP3</td>
</tr>
}
</table>
如果我在 'od' 列表中有一些 'TB_OUTROS_DADOS',我需要检查 'foreach' 的内部。
谢谢!
你不能以这种方式做到这一点。 将 List<> 作为模型传递给您的父视图,或者以与在局部视图中相同的方式声明此列表,然后进行检查,如果合适,则将其传递给局部视图。 这样:
@{
List<TB_OUTROS_DADOS> od = (List<TB_OUTROS_DADOS>)ViewData["outrosDados"];
<div class="table-responsive">
@{
if (od.Count > 0){
Html.Partial("~/Views/Outrosdados/Search.cshtml",od);
}
else
{
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
</table>
}
}
</div>
并且在 PartialView 中
@model List<TB_OUTROS_DADOS>
@using EixoX.Html;
@using Gestão.Models;
@using System.Globalization;
@{
Bootstrap3Presenter<TB_OUTROS_DADOS> presenter =
Bootstrap3Presenter<TB_OUTROS_DADOS>.GetInstance("pt-BR");
ViewBag.Title = "Outros Dados";
}
<table class="table table-striped">
<tr>
<th style="min-width:130px;">Mês</th>
<th style="min-width:80px;">Amortização</th>
<th style="min-width:100px;">Recebimento do Mês</th>
<th style="min-width:100px;">Atraso</th>
<th style="min-width:100px;">DAMP3</th>
</tr>
@foreach (TB_OUTROS_DADOS dados in Model){
<tr>
<td>@dados.DT_MES_UTILIZACAO.ToString(CultureInfo.CreateSpecificCulture("pt-BR"))</td>
<td><span class="money" />@dados.VL_AMORTIZACAO</td>
<td><span class="money" />@dados.VL_RECEBIMENTO_MES</td>
<td><span class="money" />@dados.VL_ATRASO</td>
<td><span class="money" />@dados.VL_DAMP3</td>
</tr>
}
</table>