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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:02:04+00:00 2026-05-17T01:02:04+00:00

I have a partial view that renders a list of objects into a table

  • 0

I have a partial view that renders a list of objects into a table format and allows editing of the values…

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<whoozit.Models.PictureModel>>" %>

<% foreach (whoozit.Models.PictureModel p in Model)
           { %>

  <td>
  <%: Html.TextBox("name",p.name) %>
  <%: Html.ValidationMessage(p.name) %>
  </td>  

<% } %>

I’m wanting to refactor this to take advantage of the strongly typed html helpers in mvc2. I am running into difficulty understanding how to create the lambda expressions and was hoping for some help. the following doesn’t seem quite correct to me.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<whoozit.Models.PictureModel>>" %>

<% foreach (whoozit.Models.PictureModel p in Model)
           { %>

  <td>
  <%: Html.TextBoxFor(???) %>
  </td>  

<% } %>
  • 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-17T01:02:05+00:00Added an answer on May 17, 2026 at 1:02 am

    First of all you shouldn’t be iterating in a view. Iterating means loops, loops mean C#/VB.NET, C#/VB.NET in a view leads to spaghetti code.

    I would recommend you using Editor Templates. This way you don’t need to write loops in your views. Add the following file in ~/Views/Home/EditorTemplates/PictureModel.ascx:

    <%@ Control Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<whoozit.Models.PictureModel>" %>
    <td>
        <%: Html.TextBoxFor(x => x.name) %>
        <%: Html.ValidationMessageFor(x => x.name) %>
    </td>
    

    Notice that the partial is now strongly typed to whoozit.Models.PictureModel instead of IList<whoozit.Models.PictureModel>. Now all that is left is to include this partial from the main view:

    <%: Html.EditorFor(x => x.Pictures) %>
    

    Where Pictures is a property of type IList<whoozit.Models.PictureModel> on your main view model. This will automatically invoke the partial for each element of the collection so that you don’t need to write ugly loops in your views.

    It just works by convention: the partial needs to be called PictureModel.ascx as the type name of the list elements and located in ~/Views/Home/EditorTemplates or ~/Views/Shared/EditorTemplates folder.

    Editor/Display templates will make your views much more elegant.

    Remark: In .NET the convention is property names to start with capital letter, so I would recommend you renaming the name property to Name. It’s just feels more natural to write and read:

    <%: Html.TextBoxFor(x => x.Name) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I have a view render a partial (user control) from a different
I have a partial that renders a select box using the following method: <%=
I have a control that is created like so: public partial class MYControl :
I have read about partial methods in the latest C# language specification , so
I have a strongly typed user control (partial) and I'd like to be able
I have dbml with single table users i want add partial class for User
Anyone knows if is possible to have partial class definition on C++ ? Something
I have this partial code: if ($getRecords = $con->prepare(SELECT * FROM AUCTIONS WHERE ARTICLE_NO
I'm trying to make an AJAXy submission and have the resulting partial be inserted
I have been using the partial class modifier for some time in order to

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.