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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:26:08+00:00 2026-05-31T11:26:08+00:00

C# valid ModelState fails .isValid Search Controller // // GET: /Search/Create public ActionResult Create()

  • 0

C# valid ModelState fails .isValid

Search Controller

//
// GET: /Search/Create
public ActionResult Create()  { return View();  } 

//
// POST: /Search/Create
[HttpPost]
public ActionResult Create(Search search)
{
    search.Created = DateTime.Now;

    search.SearchSet = "test data";
    search.URLParameter = 1432567389;

    if (ModelState.IsValid)
    {
        _db.Searchs.Add(search);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(search); 
}

Search Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace TalentProfile.Models
{
    public class Search
    {
        public int Id { get; set; }
        [Required]
        public int URLParameter { get; set; }
        [Required, MaxLength(50, ErrorMessage = "Client must be 50 characters or less")]
        public string Client { get; set; }
        [DataType(DataType.MultilineText), StringLength(150, ErrorMessage = "{0} must be {1} characters or less")]
        public string Notes { get; set; }
        [Required]
        public string SearchSet { get; set; }
        [Required]
        public DateTime Created { get; set; }
    }
}

If I run in debug all the fields in the class are properly set but it fails ModelState.IsValid. If I drill into the ModelState.IsValid it is false. Drilling down further I find the error “The SearchSet field is required”.
The SearchSet field is properly set to “test data”. If I remove the ModelState.IsValid check the save to the database succeeds.

Why am I getting the “field is required” error if the field contains valid data.

Talent Controller

//
// GET: /Talent/Create
public ActionResult Create()
{
    return View();
}

//
// POST: /Talent/Create
[HttpPost]
public ActionResult Create(Talent talent)
{
    talent.Modified = talent.Created = DateTime.Now;

    if (ModelState.IsValid)
    {
        _db.Talents.Add(talent);
        _db.SaveChanges();
        CreatePhoto(talent.Id);
        return RedirectToAction("Index");
    }

    return View(talent);
}

Updated: Search Controller and Create View

//
// GET: /Search/Create
public ActionResult Create()
{ 
    Search search = new Search();
    search.SearchSet = "test Data";
    return View(search);
}

//
// POST: /Search/Create
[HttpPost]
public ActionResult Create(Search search)
{
    search.Created = DateTime.Now;

    search.URLParameter = 1435267836;

    if (ModelState.IsValid)
    {
        _db.Searchs.Add(search);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(search);
}

In view put: @Html.HiddenFor(model => model.SearchSet)

  • 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-31T11:26:10+00:00Added an answer on May 31, 2026 at 11:26 am

    There are a couple of things to note here:

    1) putting a Required attribute on value types is pointless. Value types must always contain a value, they can’t be null.. thus Required will always pass for them.

    In particular, DateTime is a value type. Also, your UrlParameter is an int, which is also a value type. it will always contain at least a default value. Required is redundant.

    2) As others have said, the ModelState is only set during databinding. data binding only occurs before the method is called, or when you call UpdateModel or TryUpdateModel. Regardless of whether or not you have other errors, you cannot update the model and expect the ModelState to reflect the changes if you have not called UpdateModel or TryUpdateModel.

    3) Client is set as required, but you don’t seem to be setting it. So if you set Client and SearchSet then do TryUpdateModel(search) it should be valid.

    4) Talent works because, like in Search, talent.Modified and talent.Created are DateTimes and are non-nullable, thus they will never fail validation, even if you don’t set a value (see for yourself, remove the assignment and see how it still validates). Any other required fields you may have on Talent are likely also value types.

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

Sidebar

Related Questions

When testing my controller's actions the ModelState is always valid. public class Product {
When a form is posted in my controller, I make the following check: if(ModelState.IsValid)
i have this controller: public ActionResult Novo() { var products = context.Product.Select(x => new
I have this property in my view model: [DisplayName(Region)] public int? RegionId { get;
Given an existing valid SVG document, what's the best way to create informational popups,
Create a file called Valid[File].txt and stick some text in it. Start powershell and
I'm having difficulties to understand what happens when ModelState is not valid (EF4.1, database
I cannot find this library anywhere. I googled MVCSerializer I didn't get any valid
Problem: ModelState is not valid, complaining that there is no Event. I've tried associating
Consider the following model and controller: public class SimpleModel { [Required(ErrorMessage=Email Address is required.)]

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.