2017-06-08 3 views
0

Freunde,Get Objekt in Teilansicht

Ich habe eine View-Seite namens "OtherDatas.cshtml" und ich muss eine PartialView in ihr rendern. Um dies zu erreichen, muss ich überprüfen, ob das Objekt innerhalb von partialView null ist oder nicht. Wenn nicht, erscheint die partielle Ansicht. Wie kann ich das Objekt innerhalb der Teilansicht erhalten? Hier ist mein 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> 

ich in der 'foreach' überprüfen müssen wenn ich ein 'TB_OUTROS_DADOS' in der 'od' Liste habe.

Danke!

+0

Fügen Sie die If-Anweisung, die Sie auf der Hauptseite auf die Teilansicht haben. Im Teil können Sie überprüfen, ob das Objekt existiert oder nicht. Wenn es existiert, geben Sie die Daten aus der Teilansicht zurück, andernfalls geben Sie das zurück, was Sie gerade in Ihrer else-Anweisung in der Hauptansicht haben – Simon

Antwort

1

Sie können das nicht auf diese Weise tun. Pass-Liste <> als Modell für Ihre Elternansicht oder deklarieren Sie diese Liste auf die gleiche Weise wie in der Teilansicht, dann überprüfen Sie, und wenn es gut ist, übergeben Sie es zur Teilansicht. Auf diese Weise:

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

Und 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> 
+0

Vielen Dank. Es hat für mich funktioniert! Ich denke, ich versuche das Unmögliche zu machen, haha – Csorgo