Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc ViewBag - Cannot convert null to 'bool' because it is a non-nullable value type

I want to set a bool to true in the controller when producing a certain view and then alter the header of the view accordingly. This should be dead simple but instead Im getting:

Cannot perform runtime binding on a null reference Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

All I'm doing is in controller:

[AllowAnonymous]
public ActionResult Register()
{
    ViewBag.IsRegistration = true;
    return View();
}

and then in view:

@if (ViewBag.IsRegistration)
{
    <legend>Register using another service.</legend>
}
else
{
    <legend>Use another service to log in.</legend>
}

but it fails on:

@if (ViewBag.IsRegistration)

UPDATE

Relevant Controller Code:

[AllowAnonymous]
public ActionResult Register()
{
    ViewBag.IsRegistration = "true";
    return View();
}

The register view:

@model Mvc.Models.RegisterViewModel
@{
     Layout = "~/Views/Shared/_AccountLayout.cshtml";
     ViewBag.Title = "Register";
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
</hgroup>

<div class="row">
<div class="col-lg-6">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()

        <fieldset class="form-horizontal">
            <legend>Create a new account.</legend>
            <div class="control-group">
                @Html.LabelFor(m => m.UserName, new { @class = "control-label" })
                <div class="controls">
                    @Html.TextBoxFor(m => m.UserName)
                </div>
            </div>
            <div class="control-group">
                @Html.LabelFor(m => m.Password, new { @class = "control-label" })
                <div class="controls">
                    @Html.PasswordFor(m => m.Password)
                </div>
            </div>
            <div class="control-group">
                @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
                <div class="controls">
                    @Html.PasswordFor(m => m.ConfirmPassword)
                </div>
            </div>
            <div class="form-actions no-color">
                <input type="submit" value="Register" class="btn" />
            </div>
        </fieldset>
    }
</div>
    <div class="col-lg-6"></div>
  <section id="socialLoginForm">
            @Html.Action("ExternalLoginsList", new { ReturnUrl = ViewBag.ReturnUrl })
        </section>
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

The ExternalLoginsList partial:

@using Glimpse.Core.Extensions
@using Microsoft.Owin.Security
@model ICollection<AuthenticationDescription>

@if (Model.Count == 0)
{
    <div class="message-info">
        <p>There are no external authentication services configured</p>
    </div>
}
else
{
    using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = ViewBag.ReturnUrl }))
    {
    @Html.AntiForgeryToken()

        <fieldset id="socialLoginList">
            @if (!string.IsNullOrEmpty(ViewBag.IsRegistration))
            {
            <legend>Register using another service.</legend>
            }
            else
            {
            <legend>Use another service to log in.</legend>
            }
            <p>
                @foreach (AuthenticationDescription p in Model) {
                    <button type="submit" class="btn" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
                }
            </p>
        </fieldset>
    }
}
like image 629
rism Avatar asked Oct 23 '13 22:10

rism


2 Answers

Try:

@if (ViewBag.IsRegistration == true)
like image 88
acfrancis Avatar answered Oct 10 '22 04:10

acfrancis


I know this is an old question, but I think I have an elegant answer, so in case anyone reads this after searching, here is mine:

@if (ViewBag.IsRegistration ?? false)
like image 39
jfren484 Avatar answered Oct 10 '22 04:10

jfren484