0

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?

1
  • 1
    There's nothing wrong with that. Commented Oct 24, 2011 at 17:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.