2

Or maybe its the other way around?

I am new MVC so be gentle. I can select a value from a Html.DropDownList and it populates to the model when saved. However if I click out of the box jQuery.Validate barks at me with an error about null data.

This is the error

+       this    function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    }   Object, (Function)

This is the view

@model Game.Domain.Equipment

<div class="editor-label">
    @Html.LabelFor(model => model.BaseGameObject.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.BaseGameObject.Name) @Html.ValidationMessageFor(model => model.BaseGameObject.Name)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.BaseGameObject.Description)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.BaseGameObject.Description) @Html.ValidationMessageFor(model => model.BaseGameObject.Description)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Type)
</div>

<div class="editor-field">
    @Html.DropDownListFor(model => model.Type, new SelectList(Enum.GetValues(typeof(Game.Domain.EquipmentType))))
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Value)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Value) @Html.ValidationMessageFor(model => model.Value)
</div>

This is the model

namespace Game.Domain
{
    public class Equipment
    {
        public Equipment()
        {
            BaseGameObject = new BaseGameObject();
        }

        public virtual int Id { get; set; }
        public virtual BaseGameObject BaseGameObject { get; set; }
        public virtual EquipmentType Type { get; set; }

        public virtual int Value { get; set; }
    }
}

I have attempted to turn off the jvalidate jquery with jquery

$(function () {
    var select = $('select');
    $('select').attr("willValidate", false)
});

But no matter what I try I get same jvalidate error over and over. Its damned annoying =)

Sparky
  • 98,165
  • 25
  • 199
  • 285
Siegeon
  • 610
  • 3
  • 15
  • 33
  • am I right to understand that your form submits as long as you pick a value in your select box and that your issue is the error msg flashing if u unselect the value before submitting? – Dave Alperovich Jan 29 '13 at 04:56
  • Yes, when I click submit the error is thrown before the model saves back to the database. – Siegeon Jan 29 '13 at 04:59
  • do you have [required] annotation in your model applied to the id picked by the drop down? is a value selcted when you hit submit and get a validation error? – Dave Alperovich Jan 29 '13 at 05:00
  • are you populating either of the int id s? and do you leave the dropdown unselected when the validation error comes up? – Dave Alperovich Jan 29 '13 at 05:02
  • I do not have a required annotation in the model. When I hit submit i get the mentioned error first, as the drop down list is unselected. If I continue The value saves correctly to the database. I do not populate either id, I leave that to EF, and the value changes in the dropdown list successfully, its when I leave the field. – Siegeon Jan 29 '13 at 05:07
  • funky situation you ran into. non-nullable ints applied with MVC helpers automatically get [required]. I've lost countless hours in many instances over this icky problem. – Dave Alperovich Jan 29 '13 at 05:12
  • 1
    Also note that dropdowns usually get a hidden field rendered next to them with same name so make sure your validate is looking at the right element by using ID or another selector or put an ignore on it via ignore option in validate set to the selector. – Farrukh Subhani Jan 29 '13 at 05:23
  • I'm not getting this error. I don't think it is to do with nullable ints. What versions of MVC, jQuery and the 'unobtrusive' script are you using? – politus Jan 29 '13 at 11:00
  • I've had this happen with MVC 2,3,4. Nullable int's will always cause a field to behave like it is [required]. but @politus, I can't speak for your situation w/o a code sample... – Dave Alperovich Jan 29 '13 at 11:54
  • @Siegeon Answered this very issue here: http://stackoverflow.com/questions/14822540/syntax-error-with-parsejson-during-unobtrusive-validation/14822755#comment20792118_14822755 – Boaz Feb 12 '13 at 18:53

0 Answers0