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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:30:18+00:00 2026-05-27T17:30:18+00:00

Is there a smarter way to show/hide elements conditionally (with razor) than this below?

  • 0

Is there a smarter way to show/hide elements conditionally (with razor) than this below? The view is very large and I’m concerned about maintenance:

@if(@Model.Atendimento.PrazosEEntregas.Visivel)
{
    <div>
        <h4>Prazos e entrega do serviço</h4>

        @if (!string.IsNullOrWhiteSpace(@Model.Atendimento.PrazosEEntregas.PrazoFinalizacaoServico))
        {
            <p>@Model.Atendimento.PrazosEEntregas.PrazoFinalizacaoServico</p>
        }

        @if (!string.IsNullOrWhiteSpace(@Model.Atendimento.PrazosEEntregas.PrazoRetiradaDocumento))
        {
            <p><strong>Prazo de retirar o documento:</strong> @Model.Atendimento.PrazosEEntregas.PrazoRetiradaDocumento</p>
        }

        @if (!string.IsNullOrWhiteSpace(@Model.Atendimento.PrazosEEntregas.OndeRetirarServico))
        {
            <p><strong>Onde retirar/receber:</strong> @Model.Atendimento.PrazosEEntregas.OndeRetirarServico</p>
        }

        @if (!string.IsNullOrWhiteSpace(@Model.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada))
        {
            <p><strong>Observação:</strong> @Model.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada</p>
        }
    </div>
}

Thanks,
Hoisel

  • 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-27T17:30:18+00:00Added an answer on May 27, 2026 at 5:30 pm

    You could write a custom helper that will conditionally output the contents:

    public static class HtmlExtensions
    {
        public static IHtmlString FormatValue(
            this HtmlHelper htmlHelper, 
            string value, 
            string label
        )
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return MvcHtmlString.Empty;
            }
    
            var result = string.Format(
                "<p><strong>{0}</strong> {1}</p>",
                htmlHelper.Encode(label),
                htmlHelper.Encode(value)
            );
            return new HtmlString(value);
        }
    }
    

    and then:

    @Html.FormatValue(
        Model.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada, 
        "Observação:"
    )
    

    Another possibility is to use a display template:

    @Html.DisplayFor(x => x.Atendimento.PrazosEEntregas.ObservacaoPrazoRetirada)
    

    Then you could define a display template for the string type (or a custom one):

    @model string
    @if (!string.IsNullOrWhiteSpace(Model))
    {
        <p>
            <strong>@ViewData.ModelMetadata.DisplayName</strong>
            @ViewData.TemplateInfo.FormattedModelValue
        </p>
    }
    

    and on your view model:

    [DisplayName("Observação:")]
    [UIHint("MyTemplate")]
    public string ObservacaoPrazoRetirada { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an large SQL Statement, is there any smarter way to format this
Sorry for the simple question but I feel like there's a smarter way to
There is previous little on the google on this subject other than people asking
This works, but I'm not sure why (and I'm fairly sure there's a smarter
Is there any essential difference between this code: ThreadStart starter = new ThreadStart(SomeMethod); starter.Invoke();
There is similar question on this topic I participated in it, but it doesn't
Are there any problems with this set up: I have three viewcontrollers 1. main
Are there any VS template/Starter Kit/Any example of an ASP.NET Web Application, which uses
I was wondering if there was somewhere I could get some starter kit /
There is a conversion process that is needed when migrating Visual Studio 2005 web

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.