Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:34+00:00 2026-05-26T08:39:34+00:00

I am using html.textbox for 2 of my datetime field because I need to

  • 0

I am using html.textbox for 2 of my datetime field because I need to format them in a specific format but i don’t know how to do it by html.textboxfor.
However, I realise i need to have the textboxfor for the validation in my model class to work:

  [Required(ErrorMessage = "Storage Date is required")]
  [DataType(DataType.DateTime, ErrorMessage = "Please input a valid date")]
  public DateTime StorageDate { get; set; }

Any idea how can I change my Html.Textbox below into Html.TextBoxFor with the same setting??

  @Html.TextBox("expirydate", String.Format("{0:ddd, d MMM yyyy}", DateTime.Now), new { id = "expirydate" })
    @Html.ValidationMessageFor(model => model.ExpiryDate)

Appreciate any help… Thanks…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:39:34+00:00Added an answer on May 26, 2026 at 8:39 am

    You don’t really need to use TextBoxFor() for validation to work. If your TextBox has the same id as a field in the model, the model binder will pick it up. If you’re talking about to get the unobtrusive validation features, you can always manually add the data-* attributes to your TextBox.

    However, in this case it sounds like what you really want is a custom editor, using EditorFor(). It’s a bit more work, but it will allow you to actually enforce the date/time formatting by giving the user something like a date/time picker control. The basic idea is:

    • Create a partial view called DateTime.cshtml that is bound to model of type Nullable<DateTime>, and put it into the Shared/EditorTemplates view folder.
    • Use jQuery and jQueryUI to put an HTML textbox that is styled as a date/time picker into the partial view.
    • Decorate the property on your model with the [DataType(DataType.DateTime)] attribute
    • Use Html.EditorFor(model => model.WhateverProperty)

    Fortunately, date/time pickers are probably the most popular custom MVC3 editor, so there are plenty of examples to pick from; the code from this question works fine, just make sure to note the suggestion in the answer and replace this line in the partial view:

    @inherits System.Web.Mvc.WebViewPage<System.DateTime>
    

    with this:

    @model System.DateTime?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a specific reason why I should be using the Html.CheckBox , Html.TextBox
I am using the Html.TextBox helper to create textboxes. I want to set attributes
I am using the Html.TextBox helper method to generate a textbox against my model
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
I am using asp.net MVC. I have control like <%= Html.TextBox(username) %> I want
How to disable asp:TextBox on client-side click on HTML checkbox or server-side asp:CheckBox using
I've got view: @using (Html.BeginForm(ProcessRoute, Calculator)) { @Html.TextBox(CityArrival, null, new { @style = width:
I'm using Html.BeginForm and trying to pass the supplied value of the textBox archName
I have a form in MVC: <% using (Html.BeginForm(Get, Person)) { %> <%= Html.TextBox(person_id)%>
Using HTML and CSS. I have text surrounded by a border using display:inline to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.