1

I have this ViewModel:

    [Key]
    public long KlijentID { get; set; }

    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
    [StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
    public string ImePrezime { get; set; }

    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources))]
    [StringLength(50, ErrorMessageResourceName = "StringLength50", ErrorMessageResourceType = typeof(Resources))]
    public string Adresa { get; set; }

    //Rest of the Class, not important for the question.

In the view i have:

    @using (Html.BeginForm("Edit", "Klijenti"))
    {
     @Html.HiddenFor(model => model.KlijentID)

     <div class="editor-label">
        @Html.LabelFor(model => model.ImePrezime)
     </div>
     <div class="editor-field">
        @Html.EditorFor(model => model.ImePrezime)
        @Html.ValidationMessageFor(model => model.ImePrezime)
     </div>
     <div class="editor-label">
        @Html.LabelFor(model => model.Adresa)
     </div>
     <div class="editor-field">
        @Html.EditorFor(model => model.Adresa)
        @Html.ValidationMessageFor(model => model.Adresa)
     </div>
     <p><input type="submit" value="Spremi" /><p>
    }

When i use it to update an object from database the controller action works fine:

    if (!ModelState.IsValid) throw new ValidationException();
    var k=new Klijent();
    Mapper.Map(klijent, k);
    repo.SaveKlijent(k);
    TempData["msg"] = MyResources.Properties.Resources.SaveDone;
    return RedirectToAction("Index", page);

But when i try to add a new object, than the ModelState.IsValid fails saying that the KlijentID is required. ErrorMessage:"The KlijentID field is required."

I have checked, it is set as 0 as it should be for a new object. What is the problem here?

UPDATE: I have tried to add this to my Application_Start in Global.asax

    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

The result is i still get a validation error, just now it says: ErrorMessage: "A value is required."

This is getting a bit strange, it just seems to want that value really bad. Ghosts in the machine?

Zaak
  • 482
  • 11
  • 25
  • maybe this question can help you: http://stackoverflow.com/q/4700172/576752 – frennky Nov 23 '11 at 15:11
  • Thanks for the advice, i have tried to do it that way and i have described the result in the question: didn't work. Any idea why? – Zaak Nov 23 '11 at 19:35

2 Answers2

0

I had the same problem. I set my Primary key to nullable type and everything worked like a charm.

Phantom
  • 101
  • 6
0

This is because there was no value in incoming data from form. Validation engine checks dictionary of values, and if it is not found, adds ModelState error. Value is equal to 0 because it is default value of int type.

To solve this thing, just add @Html.HiddenFor(model=>model.Id) in your Create view.

Aleksei Anufriev
  • 3,206
  • 1
  • 27
  • 31