locked
Hide / Show ASP.NET Core HTML Tags RRS feed

  • Question

  • User856008996 posted
    @if (TempData["fnOutput"] == true)
                        {
                         <span id="fnoutput"></span>
                        }

    I want to hide / show HTML tags based on the bool value in TempData. Similarly, I tried with ViewBag and it doesn't return a result. How can I hide / show HTML tags without using Javascript?

    Wednesday, June 3, 2020 9:09 PM

All replies

  • User475983607 posted

    Really the key is learning how to use the standard debugging tools.  In this case the compiler error is explaining the problem.   TempData returns an object from a dictionary.  It is up to you to cast the value to the appropriate type.

    public IActionResult Index()
    {
        TempData["fnOutput"] = true;
        return View();
    }
    @{
        ViewData["Title"] = "Home Page";
    }
    
        <div class="text-center">
            @if ((bool)TempData["fnOutput"])
            {
                <span id="fnoutput">True</span>
            }
            else
            {
                <span id="fnoutput">False</span>
            }
        </div>

    Wednesday, June 3, 2020 9:29 PM
  • User856008996 posted

    Really the key is learning how to use the standard debugging tools.  In this case the compiler error is explaining the problem.   TempData returns an object from a dictionary.  It is up to you to cast the value to the appropriate type.

    public IActionResult Index()
    {
        TempData["fnOutput"] = true;
        return View();
    }
    @{
        ViewData["Title"] = "Home Page";
    }
    
        <div class="text-center">
            @if ((bool)TempData["fnOutput"])
            {
                <span id="fnoutput">True</span>
            }
            else
            {
                <span id="fnoutput">False</span>
            }
        </div>

    I get a tempdata null error. Could you help?

    Wednesday, June 3, 2020 10:19 PM
  • User2078676645 posted

    Hi,

    I get a tempdata null error. Could you help?

    It's possible that you didn't put TempData in the correct controller, so TempData["fnOutput"] isn't assigned, you can try debug.

    Regards,

    Evern 

    Thursday, June 4, 2020 1:14 AM
  • User-1955300613 posted

    Hi,ulaskayalar

    I tried  mgebhard's advice and it works,

    you 'd better show your startup.cs code to us,so we can offer some targeted advice.

    In my opinion,you can try to put the

    app.UseCookiePolicy();

    at the bottom of the Configure().

    Welcome to show your code.

    Thursday, June 4, 2020 2:06 AM
  • User-17257777 posted

    Hi ulaskayalar,

    Where do you set the TempData and where do you use it? 

    TempData can be used to store temporary data which can be used in the subsequent request. It will be cleared out after the completion of a subsequent request.

    Best Regards,

    Jiadong Meng

    Thursday, June 4, 2020 2:40 AM
  • User475983607 posted

    I get a tempdata null error. Could you help?

    Simply check for null.

    @if (TempData["fnOutput"] != null && (bool)TempData["fnOutput"])
    {
        <span id="fnoutput">True</span>
    }
    else
    {
        <span id="fnoutput">False</span>
    }

    IF you expect TempData["fnOutput"] to have a value then you have a bug.  

    Thursday, June 4, 2020 9:53 AM