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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:45:37+00:00 2026-05-28T05:45:37+00:00

My Model class is as follow : public class PostInputViewModel { [Required] [MinLength(1)] [MaxLength(125)]

  • 0

My Model class is as follow :

public class PostInputViewModel
    {
        [Required]
        [MinLength(1)]
        [MaxLength(125)]
        public string Title { get; set; }

        [Required]
        [AllowHtml]
        [Display(Name="Content")]
        public string Content { get; set; }
    }

and controller is as follow :

[HttpPost]
        public ActionResult Write(PostInputViewModel input)
        {
            if (!ModelState.IsValid)
                return View(input);

            var post = new Post
            {
                Title = input.Title,
                Content = input.Content,
                DateCreated = DateTime.Now,
                DateModified = DateTime.MaxValue,
            };

            dbContext.Posts.Add(post);
            dbContext.SaveChanges();

            return RedirectToAction("Index", "Home");
        }

When I run web application by clicking F5, and if I don’t input title and content value, ModelState.IsValid is false, However if I test controller class with unit test case, ModelState.IsValid is always true. The test case is as follow :

[TestMethod]
        public void write_should_validate_model()
        {
            var input = new PostInputViewModel();
            input.Title = null;
            input.Content = null;
            var actionResult = controller.Write(input) as ViewResult;

            Assert.IsFalse(actionResult.ViewData.ModelState.IsValid);
        }

Am I missing something?
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-05-28T05:45:38+00:00Added an answer on May 28, 2026 at 5:45 am

    If you want to have your controller try to validate the model, you can call the TryValidateModel method before your assert:

    controller.TryValidateModel(input);
    

    But I agree that you’d really only be testing the validation attributes. It might be OK, though; it would validate that your model has the expected attributes applied.

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

Sidebar

Related Questions

Main model classes are as follows : public class UserAddressesForm { @NotEmpty private String
I have a field in my model class that's coded as follows: public string
I have a model as follows: public class User : System.Web.Security.MembershipUser { public int
I have implemented a custom Table Model as follows: public class MyTableModel extends AbstractTableModel
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50) def __unicode__(self): return self.player_name
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Assume I have a Model class called Bird and a instance of Bird called

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.