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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:28:34+00:00 2026-05-27T08:28:34+00:00

i have a partial view with a login form (with username and password fields)

  • 0

i have a partial view with a login form (with username and password fields) and a viewmodel for it with two properties with DataAnnotations attributes [Required].

On client side i have a form properly configured that is posted via ajax, and actually is reaching the action correctly.

Could you tell me if any of this two options is a best practice for ajax form post using Data Annotations to validate* or if i am totally wrong ? (i put some comments in the code related with this question)

Option A

if (ModelState.IsValid)
{
   //Do whatever with database, session, etc...
   return Json(new { error:false; }); 
}
else
{
   //The html returned by this code comes with css-class="input-validation-error"
   //for elements whose values didnt pass the validation
   //should i use Jquery in client to replace actual html of the partial view
   //by this resulting html?????
   return PartialView("Login", usersubmitted);
}

Option B

if (ModelState.IsValid)
{
   //Do whatever with database, session, etc...
   return Json(new { error:false; }); 
}
else
{
   //I could return a Json array with all validation errors that happened
   //and in client side use Jquery to add the css-class "input-validation-error"
   //to those html elements whose id matches with the values in returned array. 
   return Json(
                 new{
                      {validation-error:"UserName"},
                      {validation-error:"Password"}
                    }
               );
}
  • 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-27T08:28:34+00:00Added an answer on May 27, 2026 at 8:28 am

    Option B would be my preference as it seems to maintain a stronger separation of concerns between the View and Controller by leaving the display and rendering decisions completely in the hands of the View itself. The controller is simply informing the View of the elements that are problematic.

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

Sidebar

Related Questions

I have a Partial view (Login, with username, password and Submit button), and the
I have a partial view that is shared between two controllers and I'm trying
if i have created a view model and have a partial form that is
I'm using MVC2 with VS2010 I have a view that has two partial views
I have a partial view called Login.ascx that has my login boxes that I
My task is create both login and registration form on one view! I have
I have a layout which it has two partial views, one for LogIn and
I have a _layout page which has a login box (partial view) and that
I have a partial view that I want to basically take care of itself
I have a partial view (UserControl) that implements a simple pager in my Asp.Net

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.