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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:18:59+00:00 2026-06-04T20:18:59+00:00

In my View, I’m making an entire html table editable. Each field in the

  • 0

In my View, I’m making an entire html table editable. Each field in the table has validation. Here is some sample code…

<tbody>
    foreach (Item item in Model.Items)
    {
        <tr>
            <td>
                @Html.TextBoxFor(x => em.Value)
                @Html.ValidationMessageFor(x => em.Value)
            </td>
        </tr>
    }
</tbody>

This is creating two problems on the browser.

1) Since the name attribute for all the controls are the same, when one field is invalid, the error messages are displayed on all fields of the same name.

2) Probably related to the names as well, when validating the form, if the control in the first row is invalid, the form is invalid. But controls in all other rows will not invalidate the form (even though the error messages are displayed).

if (!$(form).valid()) {
    return false;
}

Any thoughts would be helpful.

UPDATE: THE SOLUTION

Per bobek suggestion, I created custom extensions for TextBoxInLoopFor and ValidationMessageInLoop. Below is the solution:

public static MvcHtmlString TextBoxInLoopFor<TModel, IItem, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IItem item, object htmlAttributes)
{
    var model = item as IInLoopForModel; 
    if (model == null)
        return MvcHtmlString.Empty; 

    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var fieldName = ExpressionHelper.GetExpressionText(expression);
    var name = model.Name + fieldName;

    var tag = new TagBuilder("input");
    tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    tag.Attributes.Add("id", name);
    tag.Attributes.Add("for", name);
    tag.Attributes.Add("name", name); 
    tag.Attributes.Add("value", metadata.Model.ToString());

    // Add the validation attributes
    ModelState modelState;
    if (html.ViewData.ModelState.TryGetValue(name, out modelState))
    {
        if (modelState.Errors.Count > 0)
            tag.AddCssClass(HtmlHelper.ValidationInputCssClassName);
    }
    tag.MergeAttributes(html.GetUnobtrusiveValidationAttributes(name, metadata));

    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}

public static MvcHtmlString ValidationMessageInLoopFor<TModel, IItem, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IItem item)
{
        var model = item as IInLoopForModel;
    if (model == null)
        return MvcHtmlString.Empty;

    var fieldName = ExpressionHelper.GetExpressionText(expression);
    var name = model.Name + fieldName;

    var tag = new TagBuilder("span");
    tag.Attributes.Add("class", "field-validation-valid");
    tag.Attributes.Add("data-valmsg-replace", "true");
    tag.Attributes.Add("data-valmsg-for", name);
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
  • 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-04T20:19:00+00:00Added an answer on June 4, 2026 at 8:19 pm

    This is not valid at all. MVC3 helpers for input are adding an ID for each one so here you will have multiple textboxes with same ID – it’s not valid in HTML.

    You should manually create a TextBoxFor and ValidationFor your each property on the model, or if it’s possible try:

    foreach (Item item in Model.Items)
        {
            <tr>
                <td>
                    @Html.TextBoxFor(x => item.Value)
                    @Html.ValidationMessageFor(x => item.Value)
                </td>
            </tr>
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My view: <div class=editor-label> @Html.LabelFor(model => model.UserList) </div> <div class=editor-field> @Html.ListBoxFor(model => model.SelectedUsers, new
Please view the below code as html in both firefox, and other browsers such
`//View <tr><td>Experience level</td><td><div class=editor-field> <%: Html.DropDownListFor(model => model.ExperienceLevelID, (SelectList)ViewData[Experience], --select--)%> <%: Html.ValidationMessageFor(model => model.ExperienceLevelID)
HTML view source code is: <IMG style=DISPLAY: inline src=https://admit.belgacom.be/WCE/ESW/img/load.gif /> but When i login
My view displays a series of rows in an html table. The rows are
Table view cell management has driving me crazy from past two days. Please check
By 'view' here I mean different combinations of properties of the model, not the
Root view is a UIImage View, it has subviews, those have subviews. My root
My view page has a Model which is represented by a List of Students.
My view calls some backend classes which need some user input. When user input

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.