Click here to Skip to main content
15,942,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written my jquery like this -

'''

        <script type="text/javascript">
        
        $(document).ready(function () {

            
            var IsUserVisible = '@Model.IsUserVisible';            
          
            $('#divUser').hide();
          
            if (IsUserVisible == false) {
                $('#divUser').show();
            }
            else {
                $('#divUser').hide();
            } 

        });   

</script>

'''

where the controls are -

'''
  <div class="line">
        <div class="item" id="divEmail">
            @Html.LabelFor(m => m.Email, "Email*", new { id = "email" })
            @Html.TextBoxFor(m => m.Email, new { @class = Model.IsFormatted ? "errorClass k-textbox" : "k-textbox" })
        </div>
    </div>

    <div class="line">
        <div class="item" id="divUser">
            @Html.LabelFor(m => m.Username, "User Name*", new { id = "user" })
            @Html.TextBoxFor(m => m.Username, new { @class = Model.IsFormatted ? "errorClass k-textbox" : "k-textbox" })
        </div>
    </div>

    <div class="line col_2 command">
        @{var itemWidth = "100%";}

        <div class="item rightAlign" style="width:@(itemWidth)">


            <input type="submit" value="CANCEL" id="CANCEL" class="vf-button1 button1" />
            <input type="submit" value="SUBMIT" id="SUBMIT" class="vf-button button2" />
        </div>
    </div>
'''

I have to show the #divUser on the basis of some result after submit. 

I assigned my model inside post like -



         if (some logic)
          {
            model.IsUserVisible = true;
            return View(model);
          }
                    
'''

But after submitting the username texbox is not going to be visible. Please suggest.


What I have tried:

<pre>Hi,

I have written my jquery like this -

'''

        <script type="text/javascript">
        
        $(document).ready(function () {

            
            var IsUserVisible = '@Model.IsUserVisible';            
          
            $('#divUser').hide();
          
            if (IsUserVisible == false) {
                $('#divUser').show();
            }
            else {
                $('#divUser').hide();
            } 

        });   

</script>

'''

where the controls are -

'''
  <div class="line">
        <div class="item" id="divEmail">
            @Html.LabelFor(m => m.Email, "Email*", new { id = "email" })
            @Html.TextBoxFor(m => m.Email, new { @class = Model.IsFormatted ? "errorClass k-textbox" : "k-textbox" })
        </div>
    </div>

    <div class="line">
        <div class="item" id="divUser">
            @Html.LabelFor(m => m.Username, "User Name*", new { id = "user" })
            @Html.TextBoxFor(m => m.Username, new { @class = Model.IsFormatted ? "errorClass k-textbox" : "k-textbox" })
        </div>
    </div>

    <div class="line col_2 command">
        @{var itemWidth = "100%";}

        <div class="item rightAlign" style="width:@(itemWidth)">


            <input type="submit" value="CANCEL" id="CANCEL" class="vf-button1 button1" />
            <input type="submit" value="SUBMIT" id="SUBMIT" class="vf-button button2" />
        </div>
    </div>
'''

I have to show the #divUser on the basis of some result after submit. 

I assigned my model inside post like -



         if (some logic)
          {
            model.IsUserVisible = true;
            return View(model);
          }
                    
'''

But after submitting the username texbox is not going to be visible. Please suggest.
Posted
Updated 15-May-20 23:53pm

1 solution

Ignore what is in your view, you need to look at the resulting html as that is what is running in the browser. When IsUserVisible is true it will be;

var IsUserVisible = 'True';

$('#divUser').hide();

if (IsUserVisible == false) {
    // ....
}
else {
    // ...
}


'True' doesn't equal false so your else will always run. A cheap way to get what you want would be to update the view;

var IsUserVisible = @Model.IsUserVisible.ToString().ToLowerInvariant();


I've removed the single quotes and made it lower case. You might also have a logic problem as when IsUserVisible is true you hide the div and when it is false you show it so maybe you need

if (IsUserVisible == false) {
    $('#divUser').hide();
}
else {
    $('#divUser').show();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900