关于 Asp.Net MVC Helper Html.Hidden

发布于 2024-12-11 09:11:40 字数 2377 浏览 0 评论 0原文

我有以下模型,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-12-18 09:11:40

鉴于您提供的模型,这是预期的行为。该模型将这两个属性指定为必需的,因此隐藏字段将添加不显眼的验证属性。

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.

戏舞 2024-12-18 09:11:40

隐藏并不意味着它不会被验证。
为什么不在局部视图中设置正确的日期/时间,而不是用空字符串初始化这些隐藏字段。
像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文