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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:06:40+00:00 2026-06-17T01:06:40+00:00

I have setup a little test project that I hope will explain my problem.

  • 0

I have setup a little test project that I hope will explain my problem.
I have a parent class that has a property containing all its children of type Child. In the MVC view I list all the children in a table and to have the form post back my children and map them to the parent property automatically I render them like:

@Html.TextBox(string.Format("Children[{0}].ChildName", childIndex), child.ChildName)

In the controller I have marked property ChildName to be required. The problem I have is that jquery unobtrusive validation does not emit any data validation attributes. Everything validates nice on the server but not on the client (obviously because there are no attributes for jquery validation to find on these inputs).

Please have a look at the code:

VIEW

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <form action="@Url.Action("Save")" method="post">
            @Html.TextBox("Name", (string)Model.Name)

            <table>
                <tr><td>Child name</td></tr>
                @{var childIndex = 0;}
                @foreach (var child in (List<GridValidationTest.Controllers.Child>)Model.Children)
                {
                    <tr><td>@Html.TextBox(string.Format("Children[{0}].ChildName", childIndex), child.ChildName)</td></tr>
                    childIndex++;
                }
            </table>

            <br /><button>Submit</button>
        </form>
    </div>
</body>
</html>

CONTROLLER

namespace GridValidationTest.Controllers
{
    public class Parent
    {
        [Required]
        public string Name { get; set; }
        public IList<Child> Children { get; set; }
    }

    public class Child
    {
        [Required]
        public string ChildName { get; set; }
    }

    public class MyController : Controller
    {
        //
        // GET: /My/

        public ActionResult Index()
        {
            var parent = new Parent { Name = "Parent name" };
            parent.Children = new List<Child>
            {
                new Child {ChildName = "First child"},
                new Child {ChildName = "Second child"}
            };
            return View("Index", parent);
        }

        public ActionResult Save(Parent parent)
        {
            return View("Index", parent);
        }

    }
}

The client validation on property Name of Parent class works fine as expected.
How should I render the children to have client-side validation to work as I expect?

  • 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-17T01:06:42+00:00Added an answer on June 17, 2026 at 1:06 am

    You need to bind the fields in your view to your model. Instead of using:

    @Html.TextBox(string.Format("Children[{0}].ChildName", childIndex), child.ChildName)
    

    Use

    @Html.TextBoxFor(model => model.Children[childIndex])
    @Html.ValidationMessageFor(model => model.Children[childIndex])
    

    Also make sure you are including the right jquery javascript files on your page. Something like:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    

    Also make sure that in your web config you have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled turned on like so:

    <appSettings>
        ....
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        ....
    </appSettings>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little test project set up so that when you click a
I have this nice little MSBuild-based daily build setup that I use on my
Ok here is the problem in brief. I do have a pyramid setup that
Can I get a little help with this? In a test project, I have
I am writing a little test program, and I have a problem regarding the
I have the following code setup: http://jsfiddle.net/bABHU/2/ Had to change it a little bit
I have setup an app project and a static project in a workspace in
I have a little trouble getting gettext to work. I made a simple test
I've got a problem with git and my rails project. I have set up
I'm having a little bit of a dilemma. I have a very basic class

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.