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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:39:10+00:00 2026-05-28T06:39:10+00:00

I have an order entry page and a corresponding ViewModel (simplified example): public class

  • 0

I have an order entry page and a corresponding ViewModel (simplified example):

public class OrderCreateViewModel
{
    [Required]
    [StringLength(100)]
    public string Remark { get; set; }

    [Required]
    [StringLength(50)]
    public string AddressCity { get; set; }
}

This model is used for the Create view:

@model MyNamespace.OrderCreateViewModel

@using (Html.BeginForm())
{
    @Html.ValidationSummary(false)
    @Html.EditorFor(model => model.Remark)
    @Html.EditorFor(model => model.AddressCity)
    <input type="submit" value="Create order" name="saveButton" />
}

The Create action for the POST request in the controller is:

[HttpPost]
public ActionResult Create(OrderCreateViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        // Save order in DB
        return RedirectToAction("Index");              
    }
    return View(viewModel);
}

The order (required fields and length of strings) gets validated on client side (using unobtrusive Javascript, jQuery validation) and on server side (ModelState.IsValid).

Now, I add a second button to the page which has the purpose to save the Address (AddressCity in the example) in a master table. This is supposed to happen with an Ajax POST request (using jQuery):

<input id="buttonAddAddress" type="button" value="Add address" />

A click event handler is hooked to this button which reads the content of input fields related to the address (only AddressCity in the example) and sends a POST request to the server:

$("#buttonAddAddress").click(function () {
    var address = JSON.stringify({
        City: $('#AddressCity').val()
    });

    $.ajax({
        type: 'POST',
        url: '@Url.Action("AddAddress")',
        data: address,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        ...
    });
});

The controller has a corresponding POST action for this request:

[HttpPost]
public ActionResult AddAddress(Address address)
{
    // Create address in database
    return Json(true);
}

Address is a helper type:

public class Address
{
    public string City { get; set; }
    // ...
}

This code doesn’t validate that AddressCity is required and must not be longer than 50 characters.

Question: How can I add validation to this Ajax POST request – client side and server side validation as well? I have no clue how to do it on client side. On server side I’d like to avoid the obvious trivial solution to repeat the meaning of the validation attributes (like if (!string.IsNullOrEmpty(address.City) && address.City.Length <= 50) ..., etc.) and leverage the existing validation attributes if that is possible at all.

  • 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-28T06:39:11+00:00Added an answer on May 28, 2026 at 6:39 am

    For the server:

    public class Address
    {
        [Required]
        [StringLength(50)]
        public string City { get; set; }
    }
    

    and for the client, since you are using the AddressCity field, it will already have the same validation rule as the one applied from the main view model.

    But since I guess that you will tell me that this is not DRY, well, OOP to the rescue:

    public class AddressViewModel
    {
        [Required]
        [StringLength(50)]
        public string City { get; set; }
    }
    

    and your main view model:

    public class OrderCreateViewModel: AddressViewModel
    {
        [Required]
        [StringLength(100)]
        public string Remark { get; set; }
    }
    

    and your respective controller actions:

    [HttpPost]
    public ActionResult Create(OrderCreateViewModel viewModel)
    {
        ...
    }
    
    [HttpPost]
    public ActionResult AddAddress(AddressViewModel address)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
I have a 'Purchase Order' class. It contains information about a single purchase order.
I have next tables Order , Transaction , Payment . Class Order has some
I have a show/hide Jquery function in place on a multi-entry page which toggles
I have an order entry form that has a ListBox with a list of
I have a project that requires the process of order entry. An order will
I have multiple entry points in the same module. For example I have an
I have a model Entry class Entry(db.Model): year = db.StringProperty() . . . and
I have the following code in my index view. latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry
Lets say I have data in two tables. In one I have Order ID

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.