1

我在我当前的 MVC3 项目中为我的一个模型创建了一个编辑器模板。该模型正在为底层模式的属性调用正确的编辑器,但它们缺少用于验证的不显眼的 data-val-* 属性。

如何让 MVC 发出 data-val 属性?

这是我的模型

[MetadataType(typeof(FieldTripRouteMetadata))]
public partial class FieldTripRoute
{
    private class FieldTripRouteMetadata
    {
        [Required]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime Date { get; set; }

        [Required]
        [DisplayName("Route")]
        public int RouteID { get; set; }

        [Required]            
        [DisplayName("Departure Time")]
        [UIHint("TimeWithPeriod")]
        [DisplayFormat(DataFormatString = "{0:h:mm tt}")]
        public DateTime DepartureTime { get; set; }

        [Required]
        [StringLength(255)]
        [DisplayName("Pickup Location")] 
        public String PickupLocation { get; set; }

        [Required]
        [StringLength(255)]
        public String Destination { get; set; }

        [Required]            
        [DisplayName("Arrival Time")]
        [UIHint("TimeWithPeriod")]
        [DisplayFormat(DataFormatString = "{0:h:mm t}")]
        public DateTime ArrivalTime { get; set; }

        public bool IsReturnRoute { get; set; }

        public int FieldTripID { get; set; }
    }

和我的编辑器模板:

@model FieldTripRoute

@{string routeType = (Model.IsReturnRoute ? "return" : "");}

<fieldset class="add@(routeType)Route" style="clear: both; width: 620px; margin-right: auto; margin-left: auto;">
    <legend>Add Route</legend>
    <div style="float: left; width: 275px;">
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.Date)</div>
            <div class="editor-field">@Html.EditorFor(model => model.Date)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.Date)</div>
        </div>
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.DepartureTime)</div>
            <div class="editor-field">@Html.EditorFor(model => model.DepartureTime)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.DepartureTime)</div>
        </div>
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.ArrivalTime)</div>
            <div class="editor-field">@Html.EditorFor(model => model.ArrivalTime)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.ArrivalTime)</div>
        </div>
    </div>
    <div style="float: left;">
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.RouteID)</div>
            <div class="editor-field">@Html.DropDownListFor(model => model.RouteID, Model.EditRouteList)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.RouteID)</div>
        </div>
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.PickupLocation)</div>
            <div class="editor-field">@Html.TextBoxFor(model => model.PickupLocation)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.PickupLocation)</div>
        </div>
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.Destination)</div>
            <div class="editor-field">@Html.EditorFor(model => model.Destination)</div>
            <div class="validation-error">@Html.ValidationMessageFor(model => model.Destination)</div>
        </div>          
    </div>        
    <div style="clear: both; width: 100px; margin: 20px auto 0 auto;">
        <input type="button" id="submit@(routeType)Form" name="submit@(routeType)Form" value="Add" />
    </div>
</fieldset>

我的视图模型:

public class FieldTripEditViewModel
{
    public FieldTrip Trip { get; set; }        
    public FieldTripRoute Route { get; set; }        
    public FieldTripRoute ReturnRoute { get; set; }

    public FieldTripEditViewModel(){}

    public FieldTripEditViewModel(FieldTrip trip)
    {
        this.Trip = trip;
        this.Route = new FieldTripRoute();
        this.ReturnRoute = new FieldTripRoute {IsReturnRoute = true};
    }
}

我查看了这篇文章,我所有的基本编辑器模板都对应于将名称保留为空字符串以允许 MVC 生成它的想法。然而我仍然没有验证。

4

1 回答 1

3

this.ViewContext.FormContext = new FormContext(); 通过在我的编辑器模板之前和之后添加一个表单标签并在编辑器模板的开头添加,我能够让它发挥作用。

似乎当控件传递给编辑器模板时,它会丢失当前的表单上下文。我尝试只添加 ViewContext.FormContext 命令,但如果不添加表单标签,嵌入式模型将无法验证。

所以现在我在表单中有一个表单,它按预期工作,但似乎应该有一个更简单的方法。

formContext 命令的想法来自这里

于 2012-02-17T13:52:54.900 回答