thegunner
thegunner

Reputation: 7163

Set a view's textbox (datetime field) to display readonly current datetime

I have a textbox on my view and I'm only looking it to display the current date and time(in readonly..how can this be done?

Currently I have this in the view:

    <div class="editor-field">
        @Html.EditorFor(model => model.Opened, new { @value = System.DateTime.Now, @readonly = "readonly" })
        @Html.ValidationMessageFor(model => model.Opened)
    </div>

..And this in the model:

    [Required]
    public DateTime Opened
    {
        get;
        set;
    }

How can this be implemented in MVC?

Thanks,

Upvotes: 2

Views: 1805

Answers (2)

Zafar
Zafar

Reputation: 3434

Well, you can do that, but @Angel Manuel Garcia Car's suggest is what you should be doing logically.

Anyways, here is the code. As long as you are creating a textbox, you should be using TextBoxFor.

@Html.TextBoxFor(model => model.Opened, new { @value = DateTime.Now, @readonly="readonly" })

I don't see any point here.

Upvotes: 1

anrequete
anrequete

Reputation: 85

I recommend you using a Label. I think it's unusual display dates and time at TextBoxes.

Regards.

Upvotes: 2

Related Questions