Asp.net mvc 3 将ModelMetaData复制到子属性';模型元数据

Asp.net mvc 3 将ModelMetaData复制到子属性';模型元数据,asp.net-mvc-3,validation,.net-4.0,views,Asp.net Mvc 3,Validation,.net 4.0,Views,我创建了一个包装器模型,其中包含一个内部属性Value,它是要在视图中显示的属性。在最外层的模型中,我创建了一个包装器类型的属性,并对该属性应用了一些属性和验证。在呈现包装器视图时,我希望将所有验证和属性从包装器复制到内部值属性,以便在最后调用EditorFor(m=>m.Value)时,呈现子属性的视图也呈现与容器属性相关的验证器等 HomeController.cs namespace MvcApplication1.Controllers { public class HomeCo

我创建了一个包装器模型,其中包含一个内部属性Value,它是要在视图中显示的属性。在最外层的模型中,我创建了一个包装器类型的属性,并对该属性应用了一些属性和验证。在呈现包装器视图时,我希望将所有验证和属性从包装器复制到内部值属性,以便在最后调用
EditorFor(m=>m.Value)
时,呈现子属性的视图也呈现与容器属性相关的验证器等

HomeController.cs

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new IndexModel());
        }
    }
}
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");
        }
    }
}
IndexModel.cs

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new IndexModel());
        }
    }
}
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");
        }
    }
}
摘要

最后,在呈现页面时,不包括任何验证元素等,因为在呈现String.cshtml时,它们不存在于ModelMetaData中

@* 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)