好吧,我正在研究一个我已经持续了三个月的问题。我创建了一个View,它迭代我实现IStepViewModel
的所有ViewModel。我需要在视图上显示一个表单标题,用英语表示用户正在处理的当前步骤。我想用DataAnnotations
执行此操作,因此我只需要像[StepTitle("Ownership Information")]
那样装饰每个ViewModel。我试过这样做,但我无法让它发挥作用。这意味着我的ModelDataProvider
会被调用,它会不将信息加载到metadata.AdditionalValues
中,当我的视图被加载时,我会尝试阅读ViewData.ModelMetadata.AdditionalValues["WizardStep"]
它不存在。
我会在底部包含自定义的provider
和Attribute
课程。
@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" />*@
}
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;
}
}
}
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;
}
}
}
答案 0 :(得分:0)
您可以尝试设计元数据感知属性:
public class StepTitleAttribute : Attribute, IMetadataAware
{
private readonly string _title;
public StepTitleAttribute(string title)
{
_title = title;
}
public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.AdditionalValues["title"] = _title;
}
}
然后用它装饰你的视图模型:
[StepTitle("Ownership Information")]
public class MyViewModel
{
...
}
并在相应的视图中:
<h2>@ViewData.ModelMetadata.AdditionalValues["title"]</h2>
答案 1 :(得分:0)
我不得不硬编码我的观点。这是我唯一的选择。
@switch (Model.CurrentStepIndex)
case 0:
<h2>Preparer's Information</h2>
break;
case 1:
<h2>Owner's Information</h2>
break;
case 2:
<h2>Physical Location</h2>
break;
case 3:
<h2>About this business</h2>
break;
case 4:
<h2>Describe Business</h2>
break;
case 6:
<h2>Sale or Change of Ownership</h2>
break;