c# – Mvc ViewBag – 无法将null转换为“bool”,因为它是不可为
发布时间:2020-12-15 06:22:33 所属栏目:百科 来源:网络整理
导读:我想在控制器中将bool设置为true,当产生某个视图,然后相应地更改视图的标题.这应该是死的简单,而是我得到: Cannot perform runtime binding on a null reference Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perfor
我想在控制器中将bool设置为true,当产生某个视图,然后相应地更改视图的标题.这应该是死的简单,而是我得到:
所有Im在控制器中: [AllowAnonymous] public ActionResult Register() { ViewBag.IsRegistration = true; return View(); } 然后在视图中: @if (ViewBag.IsRegistration) { <legend>Register using another service.</legend> } else { <legend>Use another service to log in.</legend> } 但它失败了: @if (ViewBag.IsRegistration) UPDATE 相关控制器代码: [AllowAnonymous] public ActionResult Register() { ViewBag.IsRegistration = "true"; return 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 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> } } 解决方法
尝试:
@if (ViewBag.IsRegistration == true) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |