Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a bool in my ViewModel called DisableNextButton and I would like to use it to disable a button in my view. I should add that this button lives inside an if statement.

Ultimately, I'm trying to add the disabled attribute when DisableNextButton is true.

Here's my unsuccessful implementation:

@if(true)
{
    <button type="button" @{ if (Model.DisableNextButton) { <text>disabled="disabled"</text> } } >Next</button>
}

I should note that this gives me a functioning enabled button.

share|improve this question
1  
possible duplicate of How to disable a button more elegantly –  Pheonixblade9 yesterday
    
"unsuccessful implementation" - What's unsuccessful about it? –  Rowan Freeman yesterday
    
@RowanFreeman he mentioned in the post that it always created an enabled button. –  Pheonixblade9 yesterday
    
I need to have this button disabled when Model.DisableNextButton is true. That's not happening with the code above. –  lobsterhat yesterday
    
I don't know what to suggest. The code works for me. .NET Fiddle –  Rowan Freeman yesterday
add comment

2 Answers

Translated from this post:

<input type="button" value="Next" 
    @{
        if(Model.DisableNextButton)
        {
            @:disabled="disabled"
        }
    }
/>

Basically, the <text> tag is unnecessary, as this is not text, it's an HTML attribute.

share|improve this answer
    
Thanks for the post. I'm getting compile errors when using @: (I'm getting } expected in line 1 column 1). Would it matter that I'm nesting if statements in Razor? –  lobsterhat yesterday
    
Do you have it exactly as typed? I don't have it open, so you may need to tweak it - line endings do matter in Razor, that's why I have it with a newline at the end. If this doesn't work and you find a tweak that does please post it as a comment and I will fix my post. –  Pheonixblade9 yesterday
    
No, I have everything on the same line. –  lobsterhat yesterday
    
Thanks for the help. When I step through the code in my view, the @disabled="disabled line gets skipped over. I verified this by putting a dummy line after in the same if statement and that gets hit. Looks like the same thing is happening to the code in my original post. –  lobsterhat yesterday
add comment

Following code may help you:

@{ 
    if (Model.DisableNextButton) {
        <button type="button"  disabled="disabled">Next</button>
    }
    else {
        <button type="button">Next</button>
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.