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 8711571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:54:36+00:00 2026-06-13T04:54:36+00:00

If a certain variable is set I want a certain field to be a

  • 0

If a certain variable is set I want a certain field to be a textbox, if it is not I want that field to simply display the value but that value is not editable.

I put some if else logic into my views and the value won’t display. Am I missing something here?

            @{
                if (isFlagSet) {
                    Html.TextBoxFor(m => m.HostpitalFinNumber, new { @Value = Model.HostpitalFinNumber });
                    Html.ValidationMessageFor(m => m.HostpitalFinNumber);
                }
                else {
                    Html.DisplayTextFor(m => m.HostpitalFinNumber);
                }
            }

Update…

I changed my code to

            @if (isFlagSet) {
                    Html.TextBoxFor(m => m.HostpitalFinNumber);
                    Html.ValidationMessageFor(m => m.HostpitalFinNumber);
            }
            else {
                    Html.DisplayFor(m => m.HostpitalFinNumber);
            }

The following is the HTML being generated. Notice how an input value is being written for Phone but not hospital fin. Hot sure why that is happening.

    <tr><td>
        <label for="Phone">Phone</label>
        </td>
        <td>
        <input Value="4124880798" id="Phone" name="Phone" type="text" value="4124880798" />
    </td></tr>
    <tr><td>
        <label for="HostpitalFinNumber">HostpitalFinNumber</label>              
        </td>
        <td>

    </td></tr>

ANSWER

I forgot to put the @ in front of my html helper and get rid of the Semi-colons.

  • 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-06-13T04:54:37+00:00Added an answer on June 13, 2026 at 4:54 am

    Your Razor syntax is wrong, the correct syntax is (you don’t need the surrounding @{ }):

    @if (isFlagSet) {
        @Html.TextBoxFor(m => m.HostpitalFinNumber)
        @Html.ValidationMessageFor(m => m.HostpitalFinNumber)
    }
    else {
        @Html.DisplayTextFor(m => m.HostpitalFinNumber)
    }
    

    The Html helpers don’t write directly to the response (except the one which name’s start with Render) so you need to use the @ to write the generated HTML into the output.

    Note that you don’t need to set the value by hand with expression new { @Value = Model.HostpitalFinNumber } because the Html.TextBoxFor will take care of it.

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

Sidebar

Related Questions

I have a certain session variable that is actually an array. I want to
I am building a menu that I want to display in a certain way.
I want to set a session variable to 0 when any of a certain
I want to show a variable (which is an image) a certain amount of
I have certain PHP class methods that access external variables. These variables are not
I have a site that allows public access to some pages, but requires a
I want to display a certain in the template only if a certain GET
The method I want to test has a local variable that references an object
I'm evaluating whether or not certain variables match expected values. The variables are set
I want to bind customize-option for a certain variable to a key, since I

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.