Search code examples
c#asp.netmodel-view-controllerviewviewbag

How to use ViewBag properties inside my ASP.NET MVC View?


I have a configuration page to configure a API request like this:

enter image description here

However when a "Klant" is not configured yet in another configuration page a API request can not be configured. Therefore I want to check if "Klant" is empty or not in my View when calling the modal. If "Klant" is empty I want to pop up an alert with the error message and if "Klant" is not empty I want to show the model above.

The modal is called by a button with the following code:

 <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Voeg API toe</a>

Than it calls this modal:

            <!--MODAL-->
        <div class="modal fade" id="myModal">
                <div class="modal-dialog" style="width: 20%;">
                    <div class="modal-content">
                        <div class="modal-header">
                            <a href="#" class="close" data-dismiss="modal">&times;</a>
                            <h3 class="modal-title">API toevoegen</h3>
                        </div>
                        <div class="modal-body">
                            <form id="myForm">
                                <p><b>Klant</b></p>
                                <p>@Html.DropDownList("Company.CompanyName", (IEnumerable<SelectListItem>)ViewBag.selectItemListCompany, new { @class = "form-control" })</p>
                                <p><b>Mapping</b></p>
                                <p>@Html.DropDownList("MappingName.Name", (IEnumerable<SelectListItem>)ViewBag.selectItemListMapping, new { @class = "form-control" })</p>
                                <p><b>API</b></p>
                                <p>@Html.TextBoxFor(model => model.FirstOrDefault().ApiName, new { @class = "form-control", @placeholder = "Naam", autocomplete = "off", Value = "" })</p>
                                <p>@Html.TextBoxFor(model => model.FirstOrDefault().ApiURL2, new { @class = "form-control", @placeholder = "URL", autocomplete = "off", Value = "" })</p>
                                <p><b>Output</b></p>
                                <p>
                                    JSON
                                    @Html.RadioButtonFor(model => model.FirstOrDefault().ResponseType, "json", new { Checked = "checked" })
                                    XML
                                    @Html.RadioButtonFor(model => model.FirstOrDefault().ResponseType, "xml")
                                </p>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>
                            <input type="reset" value="Submit" class="btn btn-success" id="btnSubmit" />
                        </div>
                    </div>
                </div>
        </div>

The DropDownList for "Klant" gets filled with a ViewBag called ViewBag.selectItemListCompany. I was thinking to check if this list is empty or not, I did this with the following code:

if(ViewBag.selectItemListCompany == null)

However this did not work,

does anoybody have other suggestions?

Thanks in advance


Solution

  • I was thinking to check if this list is empty or not, I did this with the following code:

    if(ViewBag.selectItemListCompany == null)
    

    That code is checking that the list is null, rather than empty. If you want to check the list is empty you need to see if it contains any elements. We can use .Any() for this and negate the result.

    @if (ViewBag.selectItemListCompany == null || !ViewBag.selectItemListCompany.Any())
    {
    }