0

I am using MVC for my Project. I have Page in a view and the page has some buttons in it. I want to hide some of the buttons depending on a boolean condition.

How Can I achieve that?

2 Answers 2

2

You could use a view model containing a boolean property which should indicate whether a given section should be visible or not:

@if (Model.AreButtonsVisible)
{
    <button>some button</button>
}

Another possibility is to write a custom HTML helper rendering those buttons which will take a boolean value indicating whether it should emit or not the corresponding HTML.

Sign up to request clarification or add additional context in comments.

2 Comments

could you be more specific please?
@Hari Gillala, you define a model, this model contains a boolean property, the controller action based on some condition assigns true or false to this boolean property and it passes the model to the view. In the view you test the value of the model property and based on it you include or not some portion of the HTML.
1
@if(condition){
    <input type="button" value="the button"/>
}

2 Comments

Is there any way we can hide or show the menu with Jquery rather than displaying in a new view? I am not sure, how to implement the above . could you be more specific in steps please?
Using JQery- $("#buttunId").hide()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.