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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:07:12+00:00 2026-06-07T13:07:12+00:00

*First Post I have a JQuery error handler for my Ajax posts that I

  • 0

*First Post

I have a JQuery error handler for my Ajax posts that I must use, it appends an error to the html based on the field name for that element like this

$(document).ready(function () {
function myHandler(e, error) {
    var tag = "";
    if (error.Success == true) { $('.field-validation-error').remove(); return; } // if success remove old validation and don't continue
    if (error.Success == false) { $('.field-validation-error').remove(); } // if success remove old validation and continue
    for (i = 0; i < error.Errors.length; i++) {
        var t = error.Errors[i];
        //get error key and assign it to id
        tag = t.Key;
        //clear down any existing json-validation
        for (j = 0; j < t.Value.length; j++) {
            //this part assumes that our error key is the same as our inputs name
            $('<span class="field-validation-error">' + t.Value[j].ErrorMessage + '</span>').insertAfter('input[name="' + tag + '"], textarea[name="' + tag + '"], select[name="' + tag + '"], span[name="' + tag + '"]');
        }
    }
}


$.subscribe("/******/errors", myHandler);

});

This works perfectly out of the box with our fluent validation setup until I try to add a custom modelstate error at controller level like so:

foreach (var item in model.Locations)
        {
            var cityRepos = new CityRepository(NhSession);
            var cityItem = cityRepos.GetAll().FirstOrDefault(o => o.Country.Id == item.CountryID && o.Name == item.City);
            if (cityItem == null)
                item.City
                ModelState.AddModelError("City", string.Format(@"The city ""{0}"" was not found, please ensure you have spelt it correctly. TODO: add a mail to link here with city not found subject", item.City));
        }

the problem is that the modelstate error needs to be attached to the html field name not my magic string “City”. The html name property is MVC Generated and looks something like this:

 name="Locations[0].City"

I have encountered this problem in a html helper before and used the method:

.GetFullHtmlFieldName(
                                                 ExpressionHelper.GetExpressionText(propertySelector)
                                             );

which resolved my problem in that case.

My question is can I use this method on my model property in an MVC post action to obtain the html name property it has come from?

Thanks in advance

  • 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-07T13:07:14+00:00Added an answer on June 7, 2026 at 1:07 pm

    ok so it’s not ideal but I have implemented this Helper method until I can find a better solution that doesn’t involve magic strings:

    public static class ModelStateErrorHelper
    {
        public static string CreateNameValidationAttribute(string collectionName, int index, string propertyName)
        {
            string template = "{0}[{1}].{2}";
            return string.Format(template, collectionName, index.ToString(), propertyName);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery that looks like this: $.ajax({ type: POST, url: /Customer/CancelSubscription/<%= Model.Customer.Id
I have a jQuery .ajax call: $j.ajax({ type: POST, url: /Services/Cart.asmx/UpdateQuantity, data: data, contentType:
I have two ajax calls to a web service from jquery. The first call
I have a html form, with a custom file upload field. And by that
I have a simple javascript that uses $.ajax() from JQuery which works great for
I have the following 2 jQuery Ajax calls. The first is a call to
I have a web application with HTML / jQuery which ic connected with AJAX
On the first page I have this function: <script> function update() { $(#notice_div).html('Loading..'); $.ajax({
I am using HMVC codeigniter. I am trying to use jquery ajax first time.
This is my first post and I have the feeling that this is something

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.