Okay, I’m working on a problem that I’ve been holding off on for three months. I have created a View that iterates through all of my ViewModels that implement IStepViewModel. I need to display a form title on the view that indicates in plain english the current step the user is working on. I would like to do this with DataAnnotations, so I just have to decorate each ViewModel like so [StepTitle("Ownership Information")]. I’ve tried to do this but I couldn’t get it to work. Meaning, that my ModelDataProvider would get called, it would not load the information into metadata.AdditionalValues and when my view gets loaded and I would try to read ViewData.ModelMetadata.AdditionalValues["WizardStep"] it did not exist.
I’ll include my custom provider and Attribute classes at the bottom.
Index.cshtml
@using Microsoft.Web.Mvc;
@using Tangible.Models;
@model Tangible.Models.WizardViewModel
@{
var currentStep = Model.Steps[Model.CurrentStepIndex];
var progress = ((Double)(Model.CurrentStepIndex) / Model.Steps.Count) * 100;
}
<script type="text/javascript">
$(function () {
$("#progressbar").progressbar({
value: @progress
});
});
</script>
@Html.ValidationSummary()
@using (Html.BeginForm())
{
<div id="progressbar" style="height:20px;">
<span style="position:absolute;line-height:1.2em; margin-left:10px;">Step @(Model.CurrentStepIndex + 1) out of @Model.Steps.Count</span>
</div>
<br />
if (Model.CurrentStepIndex > 0)
{
<input type="submit" value="Previous" name="prev" />
}
if (Model.CurrentStepIndex < Model.Steps.Count - 1)
{
<input type="submit" value="Save & Continue" name="next" />
}
else
{
<input type="submit" value="Finish" name="finish" />
}
@*<input type="submit" value="Save" name="Save" />*@
@Html.Serialize("wizard", Model)
@Html.Hidden("StepType", Model.Steps[Model.CurrentStepIndex].GetType())
@Html.EditorFor(x => currentStep, null, "")
if (Model.CurrentStepIndex > 0)
{
<input type="submit" value="Previous" name="prev" />
}
if (Model.CurrentStepIndex < Model.Steps.Count - 1)
{
<input type="submit" value="Save & Continue" name="next" />
}
else
{
<input type="submit" value="Finish" name="finish" />
}
@*<input type="submit" value="Save" name="Save" />*@
}
CustomAttribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Web.Mvc;
namespace Tangible.Attributes
{
public enum HtmlTextLengthAttribute
{
Description=50,
Long = 35,
Default = 60,
Short = 10,
Email = 30
}
public interface ICustomModelMetaDataAttribute
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple= false, Inherited = true)]
public sealed class WizardStepAttribute : Attribute, ICustomModelMetaDataAttribute
{
public WizardStepAttribute() : base() { }
public String Name { get; set; }
//public virtual int? Order { get; set; }
public IDictionary<string, object> WizardStepAttributes()
{
IDictionary<string, object> attribs = new Dictionary<string, object>();
//attribs = this.GetType().GetProperties().ToDictionary(p => p.Name, p=> p.GetValue(this,null)) ;
attribs.Add("Name", Name);
return attribs;
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class HtmlPropertiesAttribute : Attribute, ICustomModelMetaDataAttribute
{
public HtmlPropertiesAttribute()
{
Size = (int) HtmlTextLengthAttribute.Default;
}
public string CssClass
{
get;
set;
}
/// <summary>
/// Enter the actual number of characters you want to display in the field.
/// </summary>
public int Size
{
get;
set;
}
public IDictionary<string, object> HtmlAttributes()
{
//Todo: we could use TypeDescriptor to get the dictionary of properties and their values
IDictionary<string, object> htmlatts = new Dictionary<string, object>();
if (Size != 0)
{
htmlatts.Add("size", Size);
}
return htmlatts;
}
}
}
Custom ModelMetaDataProvider
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tangible.Attributes;
namespace Tangible.Providers
{
public class ModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, System.Type containerType, System.Func<object> modelAccessor, System.Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
var customAttr = attributes.OfType<ICustomModelMetaDataAttribute>();
if (customAttr != null)
{
foreach (var itr in customAttr)
{
metadata.AdditionalValues.Add(itr.GetType().Name, itr);
}
}
return metadata;
}
}
}
I had to hard code my my view. It was my only option.