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

  • Home
  • SEARCH
  • 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 8810839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:12:58+00:00 2026-06-14T03:12:58+00:00

i’m new in mvc and i want to show/hide a html control after a

  • 0

i’m new in mvc and i want to show/hide a html control after a jquery ajax call.

In ASP.NET is simple like set Label.Visible = false.

i have a razor view with something like this:

    @if (ViewBag.Show)
    {
        Html.Label("Test");
    }

    <input id="idClick" type="button" value="Click"/>

and a script with jquery to call an action

    $("#idClick").click(function() {
            $.ajax({
                    url: '/Test/ShowLabel',
                    type: 'GET',
                    dataType: 'json'
                    }
                });

In my index view i set:

    ViewBag.Show = false;

in the action:

    public void ShowLabel()
    {
        ViewBag.Show = true;
    }

but i can’t get the label showed

i want to avoid render the control and then apply a style to hide it

How can i do this?

  • 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-06-14T03:12:59+00:00Added an answer on June 14, 2026 at 3:12 am

    I assume the following code is also on said “index view”:

    @if (ViewBag.Show)
    {
        Html.Label("Test");
    }
    

    If that is the case, you are overwriting ViewBag.Show in the index view when you use ViewBag.Show = false;. Remove that line and it will show your textbox.

    Edit

    I just noticed your “action” is returning void–meaning no view will get rendered if this action method gets called. It seems you need to read up more on how ASP.NET MVC works, or rephrase your question.

    If you do not set ViewBag.Show = true; in the index action method (or whichever one is being used to create your view) then ViewBag.Show will default to false. Ensure you are setting the variable where it is actually being called and don’t overwrite it in your view.

    Edit 2

    In response to your edit, do not use the ViewBag at all to hide your controls. Instead, just pass in an anonymous type to the HTML helper:

    Html.Label("Test", new { style = "display: none;" })
    

    or use a CSS class instead:

    Html.Label("Test", new { @class = "some-class-with-display-none" })
    

    Edit 3

    Your question is really hard to follow and I see that you were trying to use Ajax to send back the ViewBag to the page. You can’t do this. The ViewBag is a server-side only “bag” that is only used to initially render the page. You can either return Json/Xml from your action method to indicate if the label should be hidden or visible, or use a partial page update.

    I think based on the amount of scattered information you have, it might be wise to start with synchronous pages first, and get a feel for how ASP.NET MVC works before tossing in Ajax calls.

    Edit 4

    It seems that you want to be able to change the visibility of your label based on the results of an Ajax call. Since ViewBag is not going to change a thing when your user already has the page rendered, you need to return the result back to your page via JSON, XML, or a partial page refresh. The simplest would be to return some JSON:

    public JsonResult ShowLabel ()
    {
        return Json (new { result = true }, JsonRequestBehavior.AllowGet);
    }
    

    Then add an ID to your label:

    <label id="testLabel" style="display: none">Test</label>
    

    And then make your Ajax call:

    $.ajax({
        url: '/Test/ShowLabel'
        type: 'GET',
        dataType: 'json'
    }).success(function (d) {
        if (d.result) {
            $('#testLabel').show();
        }
    });
    

    Final Edit

    Since I see you want this for validation, you can save yourself a lot of headache and use the built-in mechanisms with Remote Validation.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I want to show the soap response to UIWebview.. my soap response is, <p><img
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a

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.