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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:54:04+00:00 2026-05-12T13:54:04+00:00

I am building a .NET MVC app that has a page with a list

  • 0

I am building a .NET MVC app that has a page with a list of delete buttons, one for each item in a list. The problem I’m having is that the foreach variable “item” is not visible inside the LoginView, which results in the following error:

Compiler Error Message: CS0103: The name ‘item’ does not exist in the current context

Below is a simplified version of the view. The error occurs at the “new {id=item.Id}” in the LoggedInTemplate – the reference to “item” in the ActionLink works fine:

<% foreach (var item in Model) { %>

  <%= Html.ActionLink("Item", "Details", new { id = item.Id })%>

  <asp:LoginView runat="server">
      <LoggedInTemplate>
          <% using( Html.BeginForm( "Delete", "Items", new {id=item.Id}, FormMethod.Post)) 
          { %>
               <input type="submit" value="Delete" runat="server" />
          <% } %>
      </LoggedInTemplate>
  </asp:LoginView>
<% } %>

To clarify the problem is not that the Model has not been successfully passed to the View. The Model is visible from both inside and outside the LoginView. The foreach loop as no problem in iterating through the items in the Model (which is a List). The problem is that the iteration variable “item” is not accessible from within the LoginView – though the original Model is.

Is there any way to pass “item” through to the LoginView’s templates? Or is building LoginViews within a foreach loops the wrong way of doing things?

Is there a scoping rule that prevents using local variables within controls – perhaps because the control is rendered at a different time to the main page?

  • 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-12T13:54:04+00:00Added an answer on May 12, 2026 at 1:54 pm

    With ASP.NET MVC you really shouldn’t use user/custom controls, so if you omit the <asp:LoginView/> and write a line of code to check if the user is authenticated, you are good to go.

    Instead of your current code:

    <asp:LoginView runat="server">
        <LoggedInTemplate>
            <div>Show this to authenticated users only</div>
        </LoggedInTemplate>
    </asp:LoginView>
    

    Just use an if-statement and the value of Request.IsAuthenticated:

    <% if (Request.IsAuthenticated) { %>
        <div>Show this to authenticated users only</div>
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building my first ASP.Net MVC based app and have a problem accessing
My team is considering building our next web app using the ASP.NET MVC framework.
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've
I'm building an ASP.NET MVC application with a form that needs validation. The majority
I'm building my first ASP.NET MVC application and I am having some troubles with
I am building an Asp.net MVC site where I have a fast dedicated server
I'm building a CMS in ASP.NET MVC and want to allow users to pick
I'm building small web shop with asp.net mvc and Structuremap ioc/di. My Basket class
I am currently building an application using ASP.NET MVC. The data entry pages are
I'm am building my asp.net web application using MVC (Preview 5), and am also

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.