Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:35:52+00:00 2026-05-23T09:35:52+00:00

I am passing some viewdata and view bag to the view through controller ,

  • 0

I am passing some viewdata and view bag to the view through controller , below is the snippet of the code:

IProductRepository prodResp = new ProductRepository();
        Product getGarages = prodResp.GetDetailsForGarages((int)Session["EventID"]);
        Product getHelmets = prodResp.GetDetailsForHelmet((int)Session["EventID"]);
        if (getGarages == null)
        {

            ViewBag.Garages = null;
        }
            ViewBag.Garages = getGarages;
            int totalGarages = getGarages.QtyAvailable;
            var garages = Enumerable.Range(1, totalGarages).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewBag.GaragesDropDown = new SelectList(garages.ToList(), "Value", "Text");

            if (getHelmets == null)
            {
                ViewBag.helmets = null;
            }
            ViewBag.helmets = getHelmets;
        return View(booking);
    }

View

<% if (Convert.ToBoolean(ViewBag.boolSecondDriver))
                      {%>

                    <lable>Second Driver Availablity For this Event</lable><br />
                   <lable>Secondriver:</lable> <%: Html.TextBox("SecondDriver") %>
                    <br />
                   <lable>SecondriverPrice:</lable> <%: ViewBag.trackday.SecondDriverPrice %>
                    <br /><br />

                    <lable>Number of Helmets Available For this Event</lable><br /><br />
                    <lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
                    <lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />

                    <lable>Number of Garages Available For this Event</lable><br /><br />
                   <lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
                    <lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>

                     <%}
                       else{ %> 


                      <lable>Second Driver Availablity For this Event</lable><br />
                   <lable>Free</lable>

                    <br /><br />

                    <lable>Number of Helmets Available For this Event</lable><br /><br />
                    <lable>No of Helmets:</lable><%: ViewBag.helmets.QtyAvailable%><br />
                    <lable>Price per unit:</lable><%: ViewBag.helmets.UnitCost%> <br /><br />

                    <lable>Number of Garages Available For this Event</lable><br /><br />
                   <lable>No of Garages:</lable> <%: ViewBag.Garages.QtyAvailable%><br />
                    <lable>price per unit:</lable><%: ViewBag.Garages.UnitCost%>

                      <%} %> 

The issue I am having is I cant hide or unhide the Viewbag if the value of viewbag.value is null then the respected viewbag.values should be hidden in the view , therefore I am getting errors: Cannot perform runtime binding on a null reference. Any suggestions or alternatives will be highly appreciated thanx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:35:52+00:00Added an answer on May 23, 2026 at 9:35 am

    Any suggestions or alternatives will be highly appreciated thanx.

    Have you considered using a ViewModel?

    ViewBag is fine when dealing with single fields etc that don’t warrant a “model”, but you have a lot of code there, i see nouns like Helmet and Garage – so you should be using a ViewModel.

    Then you can use:

    <%: Html.DisplayFor(model => model.SecondDriver) %>
    

    And if SecondDriver is null nothing will be rendered.

    And you can create a display template for whatever type SecondDriver is, and move the markup there, meaning you can re-use it across any Views.

    Don’t know about you, but when i do MVC development, my #1 goal is to keep my View clean and free from code soup – which is what you currently have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've had some success passing query results to my view. Unfortunately the ASP/Razor code
Am passing some parameters through in Struts2, and am getting below error when there
iam passing some parameters in url to a page through curl, but to my
i am creating a pop up window through jsp and passing some selected values
I'm passing some data through Json to a Webservice. My problem is that i'm
I am passing some information from one view to the other using delegates, However
I'm looking for some guidance/code-snippets/tutorial on passing some simple data into my app. I
I am new to python.. In my test function I am passing some text
I'm passing some numeric arguments while creating a process (in VC++) I'm stuck at
I'm currently passing some date-time info to a web page using url parameters, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.