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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:05+00:00 2026-05-28T03:20:05+00:00

Trouble creating an (if) and (else) statement in a (View). The purpose of the

  • 0

Trouble creating an (if) and (else) statement in a (View). The purpose of the statement is to hide the following code:

<div id="clearbuton"><p>@Html.ActionLink("Clear", "")</p>

<p> @String.Format("Total of {0} results", ViewBag.CountRecords)

  </div>

Is the view the best place for hiding the code or would a controller be better suited to the task.

The code is only to be displayed if a search query is not null. The code below is the search form.

@using (Html.BeginForm())
{  <div id="borderSearch">
    @Html.TextBox("searchString", "")
    </div>
    <input type="submit" value="Search News Archives" />
}

Some code from the controller:

 if (Request.HttpMethod == "GET")
            {
                searchString = search;
            }
            else if (searchString == "")
            {
                return RedirectToAction("ErrorSearch");
            }

            else
            {
                page = 1;
            }
            ViewBag.search = searchString;

Any advice on how to accomplish this would be welcome.

  • 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-05-28T03:20:06+00:00Added an answer on May 28, 2026 at 3:20 am

    OK, I’m not 100% sure if I understand your question completely, but if I’m hearing you correctly, you’re not sure how to put an if statement around the first block of code and/or whether you should do so.

    First, here’s the “how”– you just use @if (forgive me if this seems obvious– I’m not trying to insult your intelligence):

    @if (!string.IsNullOrEmpty(ViewBag.search))
    {
        <div id="clearbuton"><p>@Html.ActionLink("Clear", "")</p>
    
        <p> @String.Format("Total of {0} results", ViewBag.CountRecords)</p>
    
        </div>
    }
    

    Now the reasoning– should you put an “if” statement in the view to show or hide HTML? Yes, absolutely. That is what the view is designed for in MVC. The controller is for doing the queries and calculating counts of results and manipulating data, but it is the view that needs to take the results and actually render them to HTML. So in this case we rely on the controller to set the value of ViewBag.search then based on that, the view can show or hide a specific block of HTML. The controller doesn’t (and shouldn’t) know about the HTML.

    Does that answer your question?

    P.S.- Here is a handy quick reference on Razor syntax if you are interested:
    http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

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

Sidebar

Related Questions

I'm currently having trouble creating an image from a binary string of data in
Having trouble creating my swap chain. I receive the following error. DX3dApp.obj : error
I am having some trouble creating a nested association and I can't quite spot
I am having trouble creating an XML document that contains a default namespace and
I'm having trouble creating a global function accessible from within all classes. I receive
I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to
I'm having trouble creating an Access table using VBA, then accessing it via open-recordset.
I am having trouble creating an entity using inout ports. I tried writing the
I am having some trouble creating a seamless photo grid. I have searched the
I am creating a Stratego Game and am having trouble creating an algorithm that

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.