0

To simplify this problem, I have written a basic if/ else statement which shows the message "The number is correct!" if the condition is met on form posting or "Sorry. The number is incorrect!" if the condition is not met.

@{
    Layout = "/shared/_SiteLayout.cshtml";

    var num1 = Request["text1"];
    var num2 = "4";
    var totalMessage = "";

    if (IsPost)
    {
        if(num1.AsInt() == num2.AsInt())
        {
            totalMessage = "The number is correct!";
        }
        else
        {
            totalMessage = "Sorry. The number is incorrect!";
        }
    }
}

<br>
<br>
<br>
<br>
<br>

<div style="margin: 0 40px 0 40px">

  <p style="font-weight: bold;">@totalMessage</p>

  <br>
  <p>4 + what = 8? &nbsp; <strong>Add the missing number</strong>.</p>
  <form action="" method="post">
    <label for="text1">Add Number Here:</label>
    <input type="text" name="text1" />
    <input type="submit" value="Add" />
  </form>
</div>

The problem: How do I style the variable below a different colour if the condition is not met?

@totalMessage

I can solve the problem by using a second variable in the statement and markup, then wrap the variable in HTML tags and add CSS styling.

var totalMessage2 = "";
totalMessage2 = "Sorry. The number is incorrect!";

<style>
.incorrect {
color: red;       
}
</style>

<span class="incorrect">@totalMessage2</span>

But, if the condition is met, the empty HTML tag still renders.

Is there another way to do this?

5
  • 1
    You could use a bool to store whether or not the number is correct. Based on the value of the bool, you could render a span with the correct class. @{ if(numberIsCorrect) { <span class="correct">@totalMessage</span> } else { <span class="incorrect">@totalMessage</span> } Commented Apr 16, 2019 at 13:01
  • When you write and post code, please be sure to use proper indention. It's extremely difficult to read code without it. I have edited your question to show how it should look. Commented Apr 16, 2019 at 13:06
  • @Kenci - This looks to be a valid answer. But, I simplified the problem from a contact form which has the conditional statement enclosed in @{ ... } at the top of the page, not in the markup. In that respect it won't work, but thanks. Commented Apr 16, 2019 at 14:03
  • The bool will still be avilable in your markup further down if you declare it at the top, use @{ ... } to access it again. I belive that you can also write HTML between those brackets. Commented Apr 16, 2019 at 20:22
  • @Kenci - Thanks. You're right and it works. I've implemented the code to provide a NuGet BotDetect CAPTCHA success or failure message in an email contact form.I'll up-vote and mark xzuttz as the answer. Commented Apr 17, 2019 at 8:19

2 Answers 2

1

As @kenci mentioned, you can do something like this:

@{
    Layout = "/shared/_SiteLayout.cshtml";

    var num1 = Request["text1"];

    var num2 = "4";
    var totalMessage = "";

    bool isCorrectNumber = false;

    if (IsPost)
    {
        if (num1.AsInt() == num2.AsInt())
        {
            totalMessage = "The number is correct!";
            isCorrectNumber = true;
        }
        else
        {
            totalMessage = "Sorry. The number is incorrect!";
            isCorrectNumber = false;
        }
    }
}

<br>
<br>
<br>
<br>
<br>

<div style="margin: 0 40px 0 40px">

    @{
        if (isCorrectNumber)
        {
            <span class="correct">@totalMessage</span>

        }
        else
        {
            <span class="incorrect">@totalMessage</span>

        }
    }
    <br>
    <p>4 + what = 8? &nbsp; <strong>Add the missing number</strong>.</p>
    <form action="" method="post">
        <label for="text1">Add Number Here:</label>
        <input type="text" name="text1" />
        <input type="submit" value="Add" />
    </form>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

Comment out IsPost it will work. You should check IsPost only in controller.

//if(IsPost){
        if(num1.AsInt() == num2.AsInt()) {
            totalMessage = "The number is correct!";
        }
        else
        {
            totalMessage = "Sorry. The number is incorrect!";
        }
    //}

4 Comments

This generates "Sorry. The number is incorrect!" on page load, because the statement is processed without posting user input
If validate fail it will return View is GET not POST
@ Hien Nguyen - 'ViewBag' does not exist in the current context. This is not MVC, but web pages. Thanks anyway.
did you change IsPostBack instead of IsPost?

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.