MVC3 data-val-* 属性未从模型编辑器模板发出
我已经为我当前的 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>
和我的 viewModel:
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 生成它的想法。但我仍然没有得到验证。
I have created an editor template for one of my models in my current MVC3 project. This model is calling the correct editors for the properties of the underlying mode, but they're missing the unobtrusive data-val-* attributes for validation.
How can I get MVC to emit the data-val attributes?
Here's my model
[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; }
}
and my Editor Template:
@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>
And my viewModel:
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};
}
}
I looked at this post and all of my base editor templates correspond to the idea of leaving the name as an empty string to allow MVC to generate it. Yet I still have no validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以通过在编辑器模板之前和之后添加表单标签并添加 this.ViewContext.FormContext = new FormContext(); 来使其正常运行。
在编辑器模板的开头。
看起来,当控件传递到编辑器模板时,它会丢失当前的表单上下文。我尝试仅添加 ViewContext.FormContext 命令,但如果不添加表单标签,嵌入式模型将无法验证。
所以现在我在表单中有一个表单,它按预期工作,但似乎应该有一种更简单的方法。
formContext 命令的想法来自此处
I was able to get it to function by adding a form tag before and after my editor template and adding
this.ViewContext.FormContext = new FormContext();
at the beginning of the Editor Template.It appears that when the control is passed to the editor template it loses the current form context. I tried just addin the ViewContext.FormContext command but without the addition of the form tags the embedded model would not validate.
So right now I have a form inside of a form and it works as intended, but it seems like there should be an easier way.
The idea for the formContext command came from here