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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:15:57+00:00 2026-06-05T23:15:57+00:00

So I have a for loop in my View that is supposed to render

  • 0

So I have a for loop in my View that is supposed to render out the input boxes. Now inside these input boxes I want to put lables that disappear when you click on them. This is all simple. Now it’s probably because my brain was wired for php first, and it has been difficult to get it to think in lambdas and object orientation, but I can’t figure out how to do this:

@{ for (int i = 0; i < 3; i++)
               {
                   <div class="editor-label grid_2">User</div>
                   Model.Users[i].UserFirstName = "First Name";
                   Model.Users[i].UserLastName = "Last Name";
                   Model.Users[i].UserEmailAddress = "Email Address";
                <div class="grid_10">
                @Html.TextBoxFor(m => Model.Users[i].UserFirstName, new { @class = "user-input" })
                @Html.TextBoxFor(m => Model.Users[i].UserLastName, new { @class = "user-input" })
                @Html.TextBoxFor(m => Model.Users[i].UserEmailAddress, new { @class = "user-input-long" })
                @Html.CheckBoxFor(m => Model.Users[i].IsUserAdmin)
                <span>&nbsp;admin?</span>
                </div>
                <div class="clear">
                </div>
               }
            }

And initialize the values for the users.

And you’re probably thinking “Of course that won’t work. You’re going to get a Null Reference Exception”, and you would be correct.

I might need to initialize them somewhere else and I don’t realize it but I’m just not sure. I’ve tried the [DefaultValue(“First Name”)] route and that doesn’t work.

I’m probably thinking about this wrong, but my brain is already shot from trying to figure out how to wire up these events to the controller, so any help would be appreciated!

  • 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-05T23:15:58+00:00Added an answer on June 5, 2026 at 11:15 pm

    Now inside these input boxes I want to put lables that disappear when you click on them

    You could use the HTML5 placeholder attribute:

    @Html.TextBoxFor(
        m => m.Users[i].UserFirstName, 
        new { @class = "user-input", placeholder = "First Name" }
    )
    

    And for older browsers that do not support this attribute use javascript to add this functionality. Here’s a blog post on this topic: http://davidwalsh.name/html5-placeholder

    There are also examples how you could use metadata to implement this directly at your view model: https://stackoverflow.com/a/5824169/29407

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

Sidebar

Related Questions

I want have a table in my view that is going to put 3
I have a for loop that creates multiple UIImageViews that I add to self.view
I have a foreach loop in a view that display a list of files
I have a custom View that is nested inside ScrollView and HorizontalScrollView like this:
I have a foreach loop in my view that is not working correctly. My
I have a tabled view in a while loop, where a user can view
I have loop designed to validate the user input on a question, it was
I have a loop that enters each non-empty text field into a database: foreach
I have view that display Question, Answers, Comments to answers and to Question. To
I have a view that receives new data from a secondary thread. Every time

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.