Viewbag check to see if item exists and write out html and value error

落爺英雄遲暮 提交于 2019-12-03 22:04:06

Don't use Response.Write. Instead do this:

@if (ViewBag.UserExists != null)
{
    <h3>@ViewBag.UserExists</h3>
}

May be useful to some one who need to check NULL as well data type of ViewBag

if (ViewBag.MyBag != null & ViewBag.MyBag is string) //int or Foo or anyObject
            {
                <div class="row">
                    <br />
                    <div class="alert alert-danger col-sm-offset-2 col-md-8">
                        @ViewBag.MyBag
                    </div>
                </div>
           }

The code could be further simplified to :

<h3>
   @(ViewBag.UserExists??"USER DOES NOT EXIST")
</h3>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!