Wednesday, August 21, 2013

DevExpress MVC GridView - The inline edit form is not closed when the Update button is pressed

Various conditions can lead to this behavior. I found an interesting ticket about this on DevExpress website:


But that's not all: when the create/update callbacks are called onto the controller, if the method leaves with an invalid ModelState, the same thing occurs. In my case, this was be design and to fix it, I simply cleared the ModelState using ModelState.Clear().

One could also iterate through the ModelState to remove errors only. The goal here is to make the ModelState valid.

More info on this Stack Overflow question:

C# Get Localized Display Annotation without object instance

If you use an annotation on a model property like this:

[Display(ResourceType = typeof(MyLocalization.CommonModelsStrings), Name = "User_LastName")]
public string LastName { getset; }


Elsewhere you can access the localized display string of the model class without instantiating it using the following method. I put it in a static helper class named ModelAnnotationsUtils for easy access:

public static string GetLocalizedDisplay<TModel>(string pPropertyName)
{
 DisplayAttribute attribute = ((DisplayAttribute)(typeof(TModel).GetProperty(pPropertyName).GetCustomAttributes(typeof(DisplayAttribute), true)[0]));
 ResourceManager rm = new ResourceManager(attribute.ResourceType);
 return rm.GetString(attribute.Name);
}

Usage:

string lastName = ModelAnnotationsUtils.GetLocalizedDisplay<Model.User>("LastName");

Thursday, August 15, 2013

DevExpress MVC GridView: Allow empty string for empty textbox cell

I've been struggling for too long on this. I hope this helps a few.

Instead of writing the usual:

settings.Columns.Add("Name");

use this:

settings.Columns.Add(col =>
{
 col.FieldName = "Name";
 var tbxProperties = col.PropertiesEdit as TextBoxProperties;
 tbxProperties.ConvertEmptyStringToNull = false;
});

Not pretty, but does work.

Friday, August 9, 2013

ASP.Net MVC Html.ValidationMessageFor Static Layout

By default, the resulting field will not display if the validation is ok, and it'll appear when it's not. There might be a case where with such behavior, you end up with dynamically changing dimensions in your layout when editing the field under dynamic validation (jquery.validate.unobtrusive.js). The trick here is to make the hidden validation message occupy space. You can do this by adding this to your style sheet:

.field-validation-validspan.field-validation-error
{
 height: 13px;
 display: block;
}

You may change "display: block" to "display: inline-block" if you don't want the message to appear under the preceding element.

As of this writing, this is the only way I've been able to achieve this, as the ValidationMessageFor() method doesn't have any setting to play with. Suggestions are welcome!

Friday, August 2, 2013

ASP.Net Razor: jquery uncaught typeerror object has no method 'live'

This can happen if you'r using  jquery.unobtrusive-ajax with jQuery 1.9+.

jquery.unobtrusive-ajax uses old api methods that had the time to become deprecated and get entirely removed. As of this writing, JQuery is now 1.10.2 and jquery.unobtrusive-ajax still uses some removed methods.

My fix was pretty simple. Get https://github.com/jquery/jquery-migrate and include it after the jquery include line. The old methods (like live()) are now back.

<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.min.js")" type="text/javascript"></script>

This should be only a temporary fix until MS gets in the real world and start using up to date APIs ;)

Hope this helps!