Foreach within a cshtml

Asked

Viewed 485 times

0

It is giving error the way I did foreach. If I put @ gives error in parser. If I withdraw gives other errors. How do I do? Below me full code.

@model Ruptura.Models.RupturaEntities

@{
    ViewBag.Title = "CadastroCargo";
    Layout = "~/Views/Shared/_LayoutBase.cshtml";
}

<link href="~/Content/Jqwidgets/jqx.base.css" rel="stylesheet" />
<link href="~/Content/Jqwidgets/jqx.summer.css" rel="stylesheet" />

<br />

<h3>Cadastro de Cargos</h3>

<br />

<div class="container">
    <div class="row">
        <div class="col-md-12">
            @*<div class="col-md-1">
                    <label for="txtCargo">Cargo:</label>
                </div>*@
            <div class="col-md-4">
                <input type="text" class="form-control col-md-6" name="txtCargo" id="txtCargo" placeholder="Digite um cargo válido">
            </div>
        </div>

        <br /><br />

        <div class="col-md-6">
            <h3 class="text-center">Unidade de negócio</h3>';
            <div class="well" style="max-height: 350px;overflow: auto;">
                <ul class="list-group checked-list-box" id="check-list-box">

                    @{
                        foreach (var rpt in Model.Apresentacao)
                        {
                            <li class="list-group-item" data-style="button" data-color="success">rpt.Unidade_Negocio</li>
                        }
                    }

                </ul>
            </div>
        </div>

        <div class="col-md-6">
            <br /><br /><br />
            <div class="well" style="max-height: 300px;overflow: auto;">
                <ul class="list-group list-box">
                    <li class="list-group-item" data-style="button">Gerente Região Norte</li>
                    <li class="list-group-item" data-style="button" data-color="success">Gerente Região Sul</li>
                    <li class="list-group-item" data-style="button" data-color="info">Gerente São Paulo</li>
                </ul>
            </div>
        </div>

        <div class="col-md-12">
            <div class="col-md-6">
                <label class="checkbox" for="ckbAtivo">
                    <input type="checkbox" checked="checked" class="checkbox large" data-label="suffix" data-label-prepend="prefix" name="ckbAtivo" id="ckbAtivo">Ativo
                </label>
            </div>
        </div>

    </div>

            <div id="content">
                <div class="listTree"></div>
                <button class="btn btn-success" onclick=" return GravaCargo();">Gravar</button>
                @Html.ActionLink("Voltar", "Index", "Home", new { }, new { @class = "btn btn-danger" })
            </div>
        </div>

        <script src="~/Scripts/CheckListBox/CheckListBox.js"></script>
        <script src="~/Scripts/CadastroCargo/CadastroCargo.js"></script>

I did that and the mistake continues:

@{
   if(Model.Apresentacao != null)
   { 
      foreach (var rpt in Model.Apresentacao){
      @:<li class="list-group-item" data-style="button" data-color="success">@rpt.Unidade_Negocio</li>
    }
 }

}

  • Tried to put only the @foreach {...} instead of opening a block?

2 answers

1

<div class="well" style="max-height: 350px;overflow: auto;">
                <ul class="list-group checked-list-box" id="check-list-box">

                    @foreach (var rpt in Model.Apresentacao)
                        {
                            <li class="list-group-item" data-style="button" data-color="success">@rpt.Unidade_Negocio</li>
                        }
                    }

                </ul>
            </div>
  • Gives this error: An Exception of type 'System.Nullreferenceexception' occurred in App_web_b0c3cso5.dll but was not handled in user code Additional information: Undefined object reference for an instance of an object.

  • I need to instantiate who, in that case.

  • in this case the Model.Presentation may be null, generating reference error, can put an if checking if it is not null at a level above the foreach to ensure ( this if it is expected that it may come null)

  • So, Presentation is an entity of my comic book and I am quite sure that it is populated. What can I do then to avoid this? Let’s say you’re right, but I say I have data. So, how do I outline this by avoiding these Nullexception errors being that there is data.

  • So I ask, what would lead to Model or Model.Presentation being null? What could happen? As I said, it’s a comic book entity that’s populated. So what could happen?

0

I resolved so:

<ul class="list-group checked-list-box" id="check-list-box">

                    @{
                        Ruptura.Models.RupturaEntities db = new Ruptura.Models.RupturaEntities();
                        var result_ap = db.Apresentacao
                            .Select(a => new { a.Codigo_Unidade_Negocio, a.Unidade_Negocio }).ToList().Distinct();


                        foreach (var item in result_ap)
                        { 
                            <li class="list-group-item" data-style="button" data-color="success">@item.Unidade_Negocio</li>
                        }
                    }
  • Mark this as the right answer, to close the topic!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.