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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:51:50+00:00 2026-05-24T17:51:50+00:00

Related questions: ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout

  • 0

Related questions:

  • ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout
  • ASP.NET MVC 3 unobtrusive client-side validation with dynamic content

I have an ASP.NET MVC view rendering a collection of items which the user can add to:-

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MySite.Models.Parent>" %>
<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <%: Html.HiddenFor(model => model.Id) %>
    <table>
        <thead>
            <tr>
                ...
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
            <% foreach (var child in Model.Children)
               {
                   Html.RenderPartial("ChildRow", child );
               } %>
        </tbody>
    </table>
    <p>
        <input type="submit" value="Save" />
        <%= Html.ActionLink<MyController>(x => x.ChildRow(), "Add another...", new { @class = "addRow" }) %>
    </p>
<% } %>

The “ChildRow” partial is as follows:-

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MySite.Models.Child>" %>
<tr class="editor-row">
    <td>
        <%  Html.EnableClientValidation(true);
            using (Html.BeginCollectionItem("Children")) { %>
        <%: Html.HiddenFor(model => model.Id)%>
    </td>
    <td>
        <%: Html.EditorFor(model => model.Value)%>
        <%: Html.ValidationMessageFor(model => model.Value)%>
        <% } %>
    </td>
</tr>

I’m using jQuery to grab the partial representing the row:-

$("a.addRow").live("click", function () {
    $.ajax({
        url: this.href,
        cache: false,
        success: function (html) {
            $(this).parents("form:first").children("table:first tbody:last").append(html);
            $.validator.unobtrusive.parse("form");
        }
    });
    return false;
});

The problem I’m having is that the client-side validation doesn’t work on rows that get added by jQuery. As you can see from my script, I’m running the validator over the form after the ajax call. As far as I can tell from looking around, the problem is that the Html.BeginForm() call isn’t on the ajax partial, and as such the validation attributes are not being added to the input elements. i.e. if I view the markup after adding a row:-

Inputs that existed at page load look like:-

<input name="Children[0a197c09-470c-4ab4-9eef-2bcc5f0df805].Value"
  class="text-box single-line" id="Children_0a197c09-470c-4ab4-9eef-2bcc5f0df805__Value"
  type="text" data-val-required="The Value field is required." data-val="true" value="Test"/>

Inputs that were added via ajax look like:-

<input name="Children[aa5a21b2-90bc-4e06-aadc-1f2032a121aa].Value"
  class="text-box single-line" id="Children_aa5a21b2-90bc-4e06-aadc-1f2032a121aa__Value"
  type="text" value=""/>

Obviously due to the nature of the form I cannot move the Html.BeginForm call onto my partial view. As the page is being loaded via ajax, neither can I hack the FormContext of my partial.

Is there another way I can enable client-side validation?

EDIT

As per counsellorben’s answer below, I set the FormContext and now the attributes are rendering correctly, however the validation still isn’t working (if I add a new row and leave the textbox blank, the first the application notices is when my breakpoint on the Edit POST action is hit).

I did some testing, and the $.validator.unobtrusive.parse function is definitely being called, the parseElement function is definitely being called the correct number of times for the new number of inputs in the table, and the line “info.attachValidation();” further down the parse function is definitely being hit. That’s as far as I’ve got. Still testing.

  • 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-24T17:51:51+00:00Added an answer on May 24, 2026 at 5:51 pm

    The problem you are experiencing is that no FormContext exists when the server is creating your new row. A FormContext must exist for unobtrusive validation attributes to be added to the generated HTML. Add the following to the top of your partial view:

    if (this.ViewContext.FormContext == null) 
    {
        this.ViewContext.FormContext = new FormContext(); 
    }
    

    counsellorben

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

Sidebar

Related Questions

I have a question related to my ASP.net MVC 3 application. My web app
This question is related to my ASP.NET MVC 2 development, but it could apply
All my questions in this topic related to asp.net 2.0 While working on some
I'm trying to ensure that visitors of my ASP.NET MVC website always have the
I have an ASP.NET MVC application where I need to allow to customers configure
I have a ASP.NET MVC site with a CAS server set up as the
Related to this question I play around with XSS issues in my ASP.NET MVC
I have a timesheet application in Asp.Net MVC, but the question should be general
Quick questions really. I am currently building a site using asp.net MVC and the
I need help figuring out how to succeed in implementing unobtrusive client-side validation of

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.