Answered by:
How to check all viewbag is not null in View

Question
-
I Need have to display all viewbag != null, even a single Viewbag is null it will Display Given error message
please any one assest me How Can I Do?????
Darshan Kumar B
Monday, March 11, 2019 7:10 AM
Answers
-
When you say "Check Multi ViewBag" are you referring to multiple properties? If so, what I would suggest is creating a partial view. You can add your condition to validate the properties that shoud not be null. Since ViewBag is a dynamic object, you could check to see if the ViewData dictionary contains the required value.
var warningMessage = new System.Text.StringBuilder(); if(!ViewData.TryGetValue("myViewBagProeprty", out object value) || value == null) { warningMessage.AppendLine("myViewBagProeprty is missing"); }
Thanks in advance, Ryan
- Proposed as answer by RyanHill-MSFTMicrosoft employee Tuesday, March 12, 2019 1:29 PM
- Marked as answer by Darshan Kumar B Thursday, July 4, 2019 2:28 PM
Tuesday, March 12, 2019 1:29 PM
All replies
-
ViewBag is of type DynamicViewDataDictionary (in System.Web.Mvc namespace). So that means you can interact with it by adding properties and referring to those same properties. You can add any object to the view bag for use on your view like this example To check for nulls, you would run a simple null check against the property.
var message = ViewBag.Message;
if (string.IsNullOrEmpty(message)) // does the property exists in the ViewBag
{
<h3>Missing ViewBag message</h3>
}
else
{
switch (message)
{
case string s:
<h3>View Bag message contains string: @s</h3>
break;
case int i:
<h3>View Bag message contains integer: @i</h3>
break;
default:
<h3>View Bag message is: @message</h3>
break;
}
}
Above, I show how you can use pattern matching to interact with the value that's in the ViewBag but you would already know what that value is. Bear in mind that ViewBag will be executed on server side.
I hope you find this helpful.
Thanks in advance, Ryan
- Edited by RyanHill-MSFTMicrosoft employee Tuesday, March 12, 2019 2:17 AM
- Proposed as answer by RyanHill-MSFTMicrosoft employee Tuesday, March 12, 2019 2:17 AM
- Unproposed as answer by RyanHill-MSFTMicrosoft employee Tuesday, March 12, 2019 1:29 PM
Tuesday, March 12, 2019 12:37 AM -
I Need to Check Multi ViewBag in single Condition
I Have Under Div 20 View Bag is There
in case single view view Bag is null i Need to Display Given Error Message
Darshan Kumar B
Tuesday, March 12, 2019 7:51 AM -
When you say "Check Multi ViewBag" are you referring to multiple properties? If so, what I would suggest is creating a partial view. You can add your condition to validate the properties that shoud not be null. Since ViewBag is a dynamic object, you could check to see if the ViewData dictionary contains the required value.
var warningMessage = new System.Text.StringBuilder(); if(!ViewData.TryGetValue("myViewBagProeprty", out object value) || value == null) { warningMessage.AppendLine("myViewBagProeprty is missing"); }
Thanks in advance, Ryan
- Proposed as answer by RyanHill-MSFTMicrosoft employee Tuesday, March 12, 2019 1:29 PM
- Marked as answer by Darshan Kumar B Thursday, July 4, 2019 2:28 PM
Tuesday, March 12, 2019 1:29 PM