This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm trying to allow entering an empty string in my SQL 2008 database via an MVC3 form and LightSpeed 4 (release build). I've followed the directions in this thread: http://www.mindscapehq.com/forums/Thread.aspx?PostID=14448 and the designer generated this code: [ValidateLength(0, 20)] [ValidatePresence(AllowEmptyString=true)] private string _extendedPartNumber; <entityProperty name="ExtendedPartNumber">
I then added this code to my create/edit view: <div class="editor-label"> Extended Part Number </div> <div class="editor-field"> @Html.EditorFor(model => model.ExtendedPartNumber) @Html.ValidationMessageFor(model => model.ExtendedPartNumber) </div If I leave ExtendedPartNumber blank and submit the form I get a validation error saying that ExtendedPartNumber is required. However, if I use code to set that value to "" in the create or edit post handlers I don't get any errors: [HttpPost] public ActionResult Create(PartNumber partNumber) { try { if (partNumber.ExtendedPartNumber == null) { partNumber.ExtendedPartNumber = ""; }
Thanks! |
|
|
From what you have describe the model binder is not unbinding the value for that field (which is why you are getting null rather than an empty string). Are the other values for the entity getting unbound correctly? If so, what does the HTTP post look like when you submit that form? One other thing you could look at is to see if you get different behavior when you use the LightSpeed custom model binder which we shipped with 4.0 - see: http://www.mindscapehq.com/documentation/lightspeed/Building-Web-Applications-/Building-ASPNET-MVC-Applications and look at the section on model binding for LightSpeed entities on how you can use the custom model binder. To be honest I would be surprised if there was different behavior as both the default MVC model binder and the LightSpeed custom model binder use the same approach to unwiring data but it would be worth checking.
Jeremy |
|
|
Thanks for the quick response. I tried the LightSpeed custom model binder and things are better, but not perfect. It looks like I'm hitting two issues. I've built up a reproduction project and uploaded it here: http://dl.dropbox.com/u/4969794/MindscapeRepros.zip In the project is an SQL CE 4.0 database with one table (Repro) and 3 nvarchar(20) properties, all set to not-nullable: LengthOnly - Has a Length <= 20 restriction LengthAndPresence - Same as LengthOnly, adds a Validate Presence requirement LenghtPresenceAndEmpty - Same as LengthAndPresence, but Validate Presence requirement is set to allow empty strings
Issue 1: Sample POST:
With the default model binder in the Create postback when an empty string is passed in for LengthOnly it is set to null and ModelState.IsValid is true, but should be false. The LightSpeed model binder returns an empty string for LengthOnly and ModelState.IsValid is true. This is proper behavior since there's no presence requirement. I'd had some trouble with the custom model binder during the beta, so I'd been a bit wary of using it. Now that it's working, I can simply remove the presence requirement and things will work properly.
Issue 2: With that said, if I leave LengthAndPresence or LengthPresenceAndEmpty blank in the Create form, the javascript validation prevents the POST from occurring and says that both properties are required when only LengthAndPresence should be required. POSTing a valid value for LengthPresenceAndEmpty and then modifiying it to the empty string in the debugger before calling Add and SaveChanges on the unit of work results in no validation errors.
In short, it looks like there's a bug in the client-side validation code that's generated when working with strings whose presence required validation is also set to allow empty strings. In my case I can work around it by removing the restriction, setting not-nullable in the database, and using the LightSpeed custom model binder. |
|
|
Unfortunately there is nothing we can do in relation to the behavior of the default model binder since this is part of MVC, that said we would certainly suggest you use the LightSpeed custom model binder as we can provide support for that! :) For the client side validation, I can see you are using the data annotation attribute helper - what will be happening here is that two data annotation attributes will be being emitted for the LengthPresenceAndEmpty property (one for the length check, one for the presence check), those are then used by MVC to generate client side validation when you are asking for it to be generated. The client side validation code is again part of MVC so there is unfortunately nothing we can do to change this, if you need some custom behavior you would need to probably forgo using the MVC generated client side validation and just directly use the jQuery Validate plugin by annotating your properties with the associated validation classes to get the behavior you need.
Jeremy |
|
|
Thanks. Building up the repro project helped straighten things out on my end. I'll definitely use the LS model binder and remove the presence restriction, since it's not really necessary when the not-null restriction is in place in the database. |
|