Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C#, Valid ModelState fails .isValid

Posted on 2012-03-15
1
Medium Priority
?
925 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

Industry Leaders: 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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

609 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