关于 Asp.Net MVC Helper Html.Hidden
我有以下模型,
Public Class BaseViewModel
Public Property Id as integer
Public Property Title As String
<DisplayName("Creation Date"), Required(), ScaffoldColumn(False)>
Public Property DateCreation As String
<DisplayName("Creation User"), Required(), ScaffoldColumn(False)>
Public Property UserCreation As String
<DisplayName("Modification Date"), ScaffoldColumn(False)>
Public Property DateModification As String
<DisplayName("Modification User"), ScaffoldColumn(False)>
Public Property UserModification As String
End Class
我创建了一个名为 BaseViewModel 的部分视图,该视图具有以下 HTML
<div id="Detail-Item" class="hidden">
@Using Html.BeginForm("Save", "Agency", Nothing, FormMethod.Post, New With {.id = "Detail-form"})
@<fieldset>
<legend>Agency</legend>
@Html.ValidationSummary()
<div class="editor-label">
@Html.LabelFor(Function(model) model.Id):
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(model) model.Id, New With {.class = "textbox-id input-key textbox-inactive", .data_field = "Id", .readonly = "readonly"})
@Html.ValidationMessageFor(Function(model) model.Id, "*")
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Title):
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(model) model.Title, New With {.class = "textbox-name input-text focus", .data_field = "Title"})
@Html.ValidationMessageFor(Function(model) model.Title, "*")
</div>
<div class="hidden">
@Html.Hidden("UserCreation", String.Empty, New With {.class = "input-audituser", .data_field = "UserCreation"})
@Html.Hidden("UserModification", String.Empty, New With {.class = "input-audituser", .data_field = "UserModification"})
@Html.Hidden("DateCreation", String.Empty, New With {.class = "input-auditdate", .data_field = "DateCreation"})
@Html.Hidden("DateModification", String.Empty, New With {.class = "input-auditdate", .data_field = "DateModification"})
</div>
</fieldset>
End Using
</div>
为了呈现此视图,我使用 @html.EditForModel。我的问题是,即使我使用 Html.Hidden 作为 DateCreation 和 UserCreation 模型属性,为这些字段生成的 Html 也包含“必需”数据验证属性。
这是它应该工作的方式吗?如果是,如何避免这种情况?
I have the following model
Public Class BaseViewModel
Public Property Id as integer
Public Property Title As String
<DisplayName("Creation Date"), Required(), ScaffoldColumn(False)>
Public Property DateCreation As String
<DisplayName("Creation User"), Required(), ScaffoldColumn(False)>
Public Property UserCreation As String
<DisplayName("Modification Date"), ScaffoldColumn(False)>
Public Property DateModification As String
<DisplayName("Modification User"), ScaffoldColumn(False)>
Public Property UserModification As String
End Class
I've created a partial view named BaseViewModel that has the following HTML
<div id="Detail-Item" class="hidden">
@Using Html.BeginForm("Save", "Agency", Nothing, FormMethod.Post, New With {.id = "Detail-form"})
@<fieldset>
<legend>Agency</legend>
@Html.ValidationSummary()
<div class="editor-label">
@Html.LabelFor(Function(model) model.Id):
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(model) model.Id, New With {.class = "textbox-id input-key textbox-inactive", .data_field = "Id", .readonly = "readonly"})
@Html.ValidationMessageFor(Function(model) model.Id, "*")
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Title):
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(model) model.Title, New With {.class = "textbox-name input-text focus", .data_field = "Title"})
@Html.ValidationMessageFor(Function(model) model.Title, "*")
</div>
<div class="hidden">
@Html.Hidden("UserCreation", String.Empty, New With {.class = "input-audituser", .data_field = "UserCreation"})
@Html.Hidden("UserModification", String.Empty, New With {.class = "input-audituser", .data_field = "UserModification"})
@Html.Hidden("DateCreation", String.Empty, New With {.class = "input-auditdate", .data_field = "DateCreation"})
@Html.Hidden("DateModification", String.Empty, New With {.class = "input-auditdate", .data_field = "DateModification"})
</div>
</fieldset>
End Using
</div>
To render this view I'm using @html.EditForModel. My problem is that even I'm using Html.Hidden for DateCreation and UserCreation model properties the Html generated for these fields contain the "required" data validation attribute.
Is this the way it should work?, if yes how can avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您提供的模型,这是预期的行为。该模型将这两个属性指定为必需的,因此隐藏字段将添加不显眼的验证属性。
Given the model you have provided, that is the expected behavior. The model designates both properties as Required, so the hidden fields will have unobtrusive validation attributes added.
隐藏并不意味着它不会被验证。
为什么不在局部视图中设置正确的日期/时间,而不是用空字符串初始化这些隐藏字段。
像 DateTime.Now 这样的东西应该可以工作。
Hidden does not mean that it will not be validated.
Why don't you just set a proper date/time in your partial view, instead of initializing those hidden fields with an empty string.
Something like DateTime.Now should work.