将ModelMetaData复制到Child属性的ModelMetaData

时间:2012-04-25 19:34:02

标签: asp.net-mvc-3 validation .net-4.0 views

我创建了一个包装器模型,它包含一个内部属性Value,它是要在视图中显示的属性。在最外层的模型中,我创建了一个属性,它是包装器的类型,我将一些属性和验证应用于该属性。当包装器视图呈现时,我想将包装器中的所有验证和属性复制到内部Value属性,这样当我最终调用EditorFor(m => m.Value)时,呈现子属性的视图也会呈现验证器等。到容器属性。

HomeController.cs

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new IndexModel());
        }
    }
}

IndexModel.cs

namespace MvcApplication1.Models.Home
{
    public class IndexModel
    {
        [Display(Name = "Test Edit Field")]
        [MaxLength(10)]
        [AdditionalMetadata("ViewName", "String")]
        [TemplateVisibility(false, true)]
        public BatchEditField<string> TestEditField { get; set; }

        public IndexModel()
        {
            this.TestEditField = new BatchEditField<string>("TEST");
        }
    }
}

EditField.cs (“包装”模型。)

namespace MvcApplication1.Models.Home
{
    public class EditField
    {
        public string RawValue { get; set; }
        public object Value { get; set; }
        public string Description { get; set; }

        public EditField() { }

        public EditField(string rawValue, object value, string description = null)
        {
            this.RawValue = rawValue;
            this.Value = value;
            this.Description = (description ?? Convert.ToString(value));
        }

        public override string ToString()
        {
            return this.Description;
        }
    }

    public class EditField<T> : BatchEditField
    {
        public new T Value { get { return (T)base.Value; } set { base.Value = value; } }

        public EditField() { }

        public EditField(string defaultValue) : base(null, defaultValue, null) { }

        public EditField(string rawValue, T value, string description = null)
            : base(rawValue, value, description)
        {
        }
    }
}

EditField.cshtml (“包装”视图。)

@using S3.Common.Extensions
@model MvcApplication1.Models.Home.EditField

@{
    //Attempting to copy AdditionalValues from the wrapper property to the child Value property.
    ModelMetadata property = this.ViewData.ModelMetadata.Properties.Single(o => "Value".Equals(o.PropertyName));

    foreach (var item in this.ViewData.ModelMetadata.AdditionalValues)
    {
        property.AdditionalValues[item.Key] = item.Value;
    }
}

@Html.EditorFor(m => m.Value, Convert.ToString(this.ViewData.ModelMetadata.AdditionalValues.ValueOrDefault("ViewName")))

@if (this.Model != null && !this.Model.RawValue.IsNullOrEmpty() && !this.Model.RawValue.Trim().IsNullOrEmpty())
{
    <p class="RawValue@(this.ViewData.ModelMetadata.AdditionalValues.ContainsKey("Style") ? " " + this.ViewData.ModelMetadata.AdditionalValues["Style"] : "")">@(this.Model.RawValue.IsNullOrEmpty() ? "(Not Specified)" : this.Model.RawValue)</p>
}

String.cshtml (子视图。)

@* Inspecting this.ViewData.ModelMetaData.AdditionalValues shows that it is empty; the parent AdditionalValues that were copied in the wrapper view did not make it through. *@
@Html.TextBox(string.Empty, this.ViewData.TemplateInfo.FormattedModelValue)
@Html.ValidationMessage(string.Empty)

摘要

最后,当呈现页面时,不包括验证元素等,因为在呈现String.cshtml时,ModelMetaData中不存在验证元素等。

0 个答案:

没有答案