?
Solved

C#, Valid ModelState fails .isValid

Posted on 2012-03-15
1
Medium Priority
?
903 Views
Last Modified: 2012-03-16
********************* 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.

Thanks,

Bob


****** 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)
0
Comment
Question by:Bobmumm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
Bobmumm earned 0 total points
ID: 37730198
When the form is posted back to the POST action a Search object is created.  Created and URLParameter are DateTime and int fields are non-nullable and are set to 01/01/0001 and 0 by default.  SearchSet is a string whose default is null, thus the ModelState fails on SearchSet.  Drilling down into ModleState shows that it only checks the string fields (Client, Notes and SearchSet), Client and Notes are set in the view.  This is why you can set the DateTime and int but not the string fields in the POST action.  Passing the view a Search object with the SearchSet field set to a non-null value in the GET action and then exposing SearchSet with a @Html.HiddenFor  resolves the problem.  See updated post with the revised code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question