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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:03+00:00 2026-06-14T17:06:03+00:00

I want to create a page to add, edit and delete a collection of

  • 0

I want to create a page to add, edit and delete a collection of objects. I want to do the add/edit/delete operations with javascript. Right now, I have the following HTML:

<table class="table table-striped">
    <thead>
        <tr>
            <td>Name</td>
            <td>Number</td>
            <td>Country</td>
            <td></td>
        </tr>
    </thead>
    <tbody>
    @foreach(var n in Model)
    {
        <tr>
            <td class="name">@n.Name</td>
            <td class="value">@n.Value</td>
            <td class="country-name">@n.CountryName</td>
            <td><ahref="#">Edit</a> | <a href="#">Delete</a></td>
        </tr>
    }
    </tbody>
</table>

<div class="modal">
    <form>
        <label>Name</label>
        <input type="text" />
        <label>Value</label>
        <input type="text />
        <button type="submit">Save</button>
    </form>
</div>

If the user presses ‘Edit’, javascript code populates the modal div with the relevant data and displays it on the screen. When the user presses ‘Submit’, the data is submited with ajax, the modal dialog is hidden, and the table is updated with the new data.

I want to validate the form before it is submitted. If possible, I would like to make use of the unobtrusive client validation system, so that changes to the data annotations of my model class are automatically reflected in the clientside code. In a more standard example, I could use the @Html.EditorFor() methods to generate the correct HTML, but this isn’t possible here.

Unobstrusive validation is controlled using attributes on the html elements, like this:

<input data-val="true" data-val-required="This field is required." id="Name"
    name="Name" type="text">
<span class="field-validation-valid" data-valmsg-for="Name"
     data-valmsg-replace="true"></span>

Is it possible to generate the relevant HTML for a particular model class without referencing a particular instance of the class? Or is there some other method I can use?

  • 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-14T17:06:04+00:00Added an answer on June 14, 2026 at 5:06 pm

    I decided to create a method to generate the data-val attributes for an input manually. Here it is:

    public static MvcHtmlString DataAttributesFor<TMdl>(Expression<Func<TMdl,object>> exp)
    {
        string prop;
        if (exp.Body is UnaryExpression)
            prop = ((exp.Body as UnaryExpression).Operand as MemberExpression).Member.Name;
        else if (exp.Body is MemberExpression)
            prop = (exp.Body as MemberExpression).Member.Name;
        else
            throw new ArgumentException("Given selector expression is unsupported.");
    
    
        var attrs = typeof(TMdl).GetProperty(prop).GetCustomAttributes();
        var dict = new Dictionary<string,string>();
        foreach (var elm in attrs)
        {
            if (elm.GetType() == typeof(RequiredAttribute))
                dict.Add("data-val-required", ((RequiredAttribute)elm).FormatErrorMessage(null));
            else if (elm.GetType() == typeof(RegularExpressionAttribute))
            {
                var re = (RegularExpressionAttribute)elm;
                dict.Add("data-val-regex", re.FormatErrorMessage(null));
                dict.Add("data-val-regex-pattern", re.Pattern);
            }
            else if (elm.GetType() == typeof(StringLengthAttribute))
            {
                var sl = (StringLengthAttribute)elm;
                dict.Add("data-val-length", sl.MaximumLength.ToString());
                dict.Add("data-val-length-max", sl.FormatErrorMessage(null));
            }
        }
    
        if (dict.Count == 0)
            return null;
    
        var sb = new StringBuilder("data-val='true'");
        foreach (var rr in dict)
            sb.Append(" " + rr.Key + "='" + rr.Value + "'");
    
        return new MvcHtmlString(sb.ToString());            
    }
    

    You use it like this:

    <input id="record-name" type="text" name="record-name"
     @(HtmlExtensions.DataAttributesFor<PhoneNumberVm>(elm => elm.Name))
    />
    

    The method is not very robust and does not work for all of the data annotation attributes, but it might provide a useful starting point for someone.

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

Sidebar

Related Questions

I want to create a html page that get data from a website table
I want to create simple html select tags with some populated values(for now what
I have a page that lets the viewer edit objects. The page displays a
In my login page I creat FA cookie. I want to add to it
I want to add a 404 page on my Django site. I've created a
i want to create a page for my clients where they enter a number
I want to create a page with will be filled with dynamic info using
I want to create a page with 2 independent columns. If the text does
I want to create settings page for my android application.In the settings page is
I want to create a web page background image that fills the entire background,

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.