user1128756
user1128756

Reputation: 79

Validation not working using @Html.HiddenFor

I have the following code:

@Html.HiddenFor(model => model.Posted, Model.Posted = DateTime.Now)

But the validation does not work It crashes when I run my application and try to edit or create an item.

public ActionResult Create()
{
    return View(new NewsItem());
}

I also have the following code above in my controller and the application it all works to what I want it do for example it inputs the current date and saves it but if I leave one editor box blank or a all the editor box's blank the application crashes and gives the following error:

Object reference not set to an instance of an object.

I do not know what this means? I thought the default validation provided by MVC3 C# should work? So what is wrong? Can anyone help?

/* This line of code is what is provided for validation it is pretty much 
   similar on all the edit, delete and create views. */
@Html.ValidationMessageFor(model => model.Posted)   

Upvotes: 0

Views: 2697

Answers (2)

ZippyV
ZippyV

Reputation: 13058

Let's keep it simple:

  • Remove any reference to Model.Posted in your view,
  • Set the Posted property to DateTime.Now() in your Post Create action in the controller.

Upvotes: 0

danludwig
danludwig

Reputation: 47375

Your second argument to Html.HiddenFor looks wrong. If you want to set the value, try this instead:

@{
    Model.Posted = DateTime.Now;
}
@Html.HiddenFor(m => m.Posted)

Or better yet, set the value in the controller:

public ActionResult Create()
{
    return View(new NewsItem { Posted = DateTime.Now });
}

Then you can just do this:

@Html.HiddenFor(m => m.Posted)

Update after comment 2

What date is being input? What should the correct date be? Your question stated a problem with a NullReferenceException (Object reference not set to an instance of an object). You may have to post the code for your NewsItem model for anyone to help you further.

Update after comment 4

I do not see where any of the code I offered change any application functionality. And if you don't understand what a NullReferenceException is, it sounds like you haven't been programming for very long. So I will take your stab at my incorrectness with a grain of salt.

A NullReferenceException has nothing to do with validation, really. It happens when you try to invoke a method, property, or other member on a variable that references null. Consider the following:

var myVariable = "Hello, world";
if (DateTime.Now.Year > 2011)
{
    myVariable = null;
}
var anotherVariable = myVariable.Substring(1);

Before Sunday, this code would have worked just fine. But as soon as the clock switches over to 2012 in your time zone, it will start to throw a NullReferenceException. Why? Because when the year is 2012 (or greater), your code will essentially be doing this:

null.SubString(1);

If this doesn't help you figure out where your NullReferenceException is coming from, then we're going to need to call in a psychic. Unless, of course, you post some more code.

Upvotes: 5

Related Questions