简体   繁体   中英

Get object inside partial View

Friends,

I have a View page called "OtherDatas.cshtml" and i need to render a PartialView inside her. So, to make this, i need to check if the object inside partialView is null or not. If not, the partialView Appears. How can i get the object inside the partialView? Here is my code:

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>

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

I need to check inside of the 'foreach' if i have some 'TB_OUTROS_DADOS' inside the 'od' list.

Thanks!

You can't do that in this way. Pass List<> as model to you parent view or declare this list in same way like in partial view then do check, and if it is good pass it to partial view. In this way:

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

And in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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