gpt4 book ai didi

c# - Mvc ViewBag - 无法将 null 转换为 'bool',因为它是不可为 null 的值类型

转载 作者:可可西里 更新时间:2023-11-01 03:02:43 25 4
gpt4 key购买 nike

我想在生成某个 View 时在 Controller 中将 bool 设置为 true,然后相应地更改 View 的标题。这应该非常简单,但我得到的是:

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

我所做的一切都在 Controller 中:

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

然后在 View 中:

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

但它失败了:

@if (ViewBag.IsRegistration)

更新

相关 Controller 代码:

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

寄存器 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")
}

ExternalLoginsList 部分:

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

最佳答案

尝试:

@if (ViewBag.IsRegistration == true)

关于c# - Mvc ViewBag - 无法将 null 转换为 'bool',因为它是不可为 null 的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com