tags:

views:

693

answers:

3

I have a view model with 2 properties that are optional - ie - not required. The view uses dropdownlistfor() to get values for these two fields, an includes an optionlabel of "" for the blank value.

When posted back to the create action the ModelState has an error for both of these fields saying "A value is required".

Anyone got any clue if this is a bug or a stupid user (ie, me) error?

Thanks

Udpate:

The View Model looks like this: [DisplayName("Check Digit Type")] public VMBarcodeMaskCheckDigitType BarcodeMaskCheckDigitType { get; set; }

    [DisplayName("Mask Type")]
    [Required(ErrorMessage="Mask type is required")]
    public VMBarcodeMaskType BarcodeMaskType
    {
        get;
        set;
    }

    [DisplayName("Product")]
    public VMProduct Product
    {
        get;
        set;
    }

The binding in the controller is :

public ActionResult Create()
        {
            BarcodeMaskViewModel model = new BarcodeMaskViewModel(new VMBarcodeMask(), Domain.GetBarcodeMaskTypes(), Domain.GetBarcodeCheckDigitTypes(), Domain.GetProducts());
            return View(model);
        }

        //
        // POST: /Barcode/Create

        [HttpPost]
        public ActionResult Create(BarcodeMaskViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {

...
                }

            }
            catch (Exception ex)
            {
                ModelState.AddModelError("*", ex);
            }
            return View(new BarcodeMaskViewModel(model.BarcodeMask, Domain.GetBarcodeMaskTypes(), Domain.GetBarcodeCheckDigitTypes(), Domain.GetProducts()));
        }
A: 

I've had this problem too, and I discovered it was actually nothing to do with the optional fields. It was because I had an auto-generating primary key column for the entity, called 'Id'. MVC2 automatically checked for a value for this, and obviously there wasn't one as it was being auto-generated.

There's an easy way to fix this is to rename the column to BarcodeId etc, rather than just Id. I gave a better explanation here: http://www.ediblecode.com/post/A-value-is-required-with-ASPNET-MVC-2.aspx

That explanation is all assuming you're using LINQ...

edibleCode
Ouch, that's nasty. In my case I'm using LightSpeed for the datalayer (which is awesome btw) and it insists on Id. My View model objects also have Id and I'm using automapper to move between the two. I'll rename the Id in the view model and change the mapping to handle this.Cheers
Jonesie
Unfortunately that didnt work :(
Jonesie
A: 

Hi,

Just use Bind(Exclude="Id") before the first parameter of your Create action.

insomniak
Sorry, that doesn't work either. I don't have a problem with Id, but with ProductId and BarcodeMaskTypeId. I tried excluding these also but that didnt work. My current workaround is to ignore any model errors from these two fields.
Jonesie
A: 

I think this a confirmed bug. See here: http://forums.asp.net/p/1529205/3699143.aspx

Juvaly