4

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

家庭控制器.cs

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new 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 中不存在。

4

0 回答 0