Tuesday, May 29, 2012
WebsitePanel MySQL error
While setting up WebsitePanel, the MySQL setup page always gives the following error.
Could not load file or assembly 'MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies
By adding the following to the web.config of C:\WebsitePanel\Server, the problem is resolved.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="6.3.7.0-6.5.4.0" newVersion="6.5.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Thursday, May 03, 2012
Forcing ValidationSummary to include property errors from IValidatableObject
I implemented IValidatableObject to my class and intended to have the errors shown in @Html.ValidationSummary(true). However, the class with the IValidatableObject is a property of the model in my view, and that causes the errors to be hidden when I exclude property errors.
There is another person on Stackoverflow with exactly the same problem, but no one has answer. http://stackoverflow.com/questions/6433023/mvc3-validationsummary-exclude-property-errors-ivalidatableobject/10433504.
To solve this, I dug out MVC3’s source code, copied and changed the ValidationSummary method to allow specific properties to be included. The following shows how to use my ValidationSummary method
@Html.ValidationSummary(new [] { "PropertyName" })
would include the property named PropertyName
@Html.ValidationSummary(new [] { "ArrayName[]" })
would include the properties ArrayName[0], ArrayName[1] etc.
@Html.ValidationSummary(new [] { "ArrayName[]", "PropertyName" })
would include both.
And here’s the code!
publicstaticMvcHtmlString ValidationSummary(thisHtmlHelper htmlHelper, string[] includePropertyErrors)
{
return ValidationSummary(htmlHelper, includePropertyErrors, null, null);
}
publicstaticMvcHtmlString ValidationSummary(thisHtmlHelper htmlHelper, string[] includePropertyErrors, string message)
{
return ValidationSummary(htmlHelper, includePropertyErrors, message, null);
}
publicstaticMvcHtmlString ValidationSummary(thisHtmlHelper htmlHelper, string[] includePropertyErrors, string message, IDictionary<string, object> htmlAttributes)
{
if (htmlHelper ==null)
{
thrownewArgumentNullException("htmlHelper");
}
FormContext formContext = htmlHelper.ViewContext.ClientValidationEnabled ? htmlHelper.ViewContext.FormContext : null;
if (htmlHelper.ViewData.ModelState.IsValid)
{
if (formContext ==null)
{ // No client side validation
returnnull;
}
// TODO: This isn't really about unobtrusive; can we fix up non-unobtrusive to get rid of this, too?
if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{ // No client-side updates
returnnull;
}
}
string messageSpan;
if (!string.IsNullOrEmpty(message))
{
TagBuilder spanTag =newTagBuilder("span");
spanTag.SetInnerText(message);
messageSpan = spanTag.ToString(TagRenderMode.Normal) +Environment.NewLine;
}
else
{
messageSpan =null;
}
StringBuilder htmlSummary =newStringBuilder();
TagBuilder unorderedList =newTagBuilder("ul");
IEnumerable<ModelState> modelStates =from ms in htmlHelper.ViewData.ModelState
where ms.Key == htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix ||
includePropertyErrors.Any(property =>
{
string prefixedProperty =string.IsNullOrEmpty(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix) ? property : htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix +"."+ property;
if (property.EndsWith("[]"))
{
return prefixedProperty.Substring(0, property.Length -2) ==Regex.Replace(ms.Key, @"\[[^\]]+\]", string.Empty);
}
else
{
return property == ms.Key;
}
})
select ms.Value;
if (modelStates !=null)
{
foreach (ModelState modelState in modelStates)
{
foreach (ModelError modelError in modelState.Errors)
{
string errorText = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError);
if (!String.IsNullOrEmpty(errorText))
{
TagBuilder listItem =newTagBuilder("li");
listItem.SetInnerText(errorText);
htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
}
}
}
}
if (htmlSummary.Length ==0)
{
htmlSummary.AppendLine(@"<li style=""display:none""></li>");
}
unorderedList.InnerHtml = htmlSummary.ToString();
TagBuilder divBuilder =newTagBuilder("div");
divBuilder.MergeAttributes(htmlAttributes);
divBuilder.AddCssClass((htmlHelper.ViewData.ModelState.IsValid) ?HtmlHelper.ValidationSummaryValidCssClassName : HtmlHelper.ValidationSummaryCssClassName);
divBuilder.InnerHtml = messageSpan + unorderedList.ToString(TagRenderMode.Normal);
if (formContext !=null)
{
if (!htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
// client val summaries need an ID
divBuilder.GenerateId("validationSummary");
formContext.ValidationSummaryId = divBuilder.Attributes["id"];
formContext.ReplaceValidationSummary =false;
}
}
returnnewMvcHtmlString(divBuilder.ToString(TagRenderMode.Normal));
}
privatestaticstring GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error)
{
returnstring.IsNullOrEmpty(error.ErrorMessage) ?null : error.ErrorMessage;
}