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 3456520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:44:47+00:00 2026-05-18T09:44:47+00:00

I have a ViewModel with a property as below: [DisplayName(As Configured On:)] [DisplayFormat(DataFormatString={0:d}, ApplyFormatInEditMode=true)]

  • 0

I have a ViewModel with a property as below:

    [DisplayName("As Configured On:")]
    [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
    public DateTime ConfigDate { get; set; }

The Form that displays the ConfigDate is as below:

<%= Html.EditorFor(Model => Model.ConfigDate)%>

When the Index Action comes back, everything looks formatted correctly, i.e. the <input> box has the date value as 12/12/2001. When the form is posted, the result that comes back is as though the DisplayFormat attribute isn’t being applied.

EDIT:
More info was requested: here is the code en toto:

The Search Form

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Config.Web.Models.AirplanesViewModel>" %>
<% using (Html.BeginForm("Details", "Airplanes", FormMethod.Post, new { id = "SearchForm" })) { %>
<%= Html.LabelFor(model => model.ConfigDate) %>
<%= Html.EditorFor(Model => Model.ConfigDate)%>
<input id="searchButton" type="submit" value="Search" />
<% } %>

The AirplanesViewModel

    public class AirplanesViewModel
{
    [DisplayName("As Configured On:")]
    [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
    public DateTime ConfigDate { get; set; }
}
}

The Controller

        [HttpGet]
    public ActionResult Index()
    {
        AirplanesViewModel avm = new AirplanesViewModel
        {
            ConfigDate = DateTime.Now
        };
        return View(avm);
    }


    [HttpPost]
    [ActionName("Details")]
    public ActionResult Details_Post(AirplanesViewModel avm)
    {
        return RedirectToAction("Details", avm);
    }


    [HttpGet]
    public ActionResult Details(AirplanesViewModel avm)
    {
        int page = 0;
        int pageSize = 10;

        if (!ModelState.IsValid)
        {
            avm.Airplanes = new PaginatedList<Airplane>();
            return View(avm);
        }

        try
        {
            Query q = new Query(avm.Query);

            PaginatedList<Airplane> paginatedPlanes = new PaginatedList<Airplane>(repo.ByQuery(q), page, pageSize);

            avm.Airplanes = paginatedPlanes;

            return View(avm);
        }
        catch (Exception)
        {
            // Should log exception
            avm.Airplanes = new PaginatedList<Airplane>();
            return View(avm);
        }
    }

Additional Information

It has something to do with the redirection to the GET Action. When I take out the POST Action and remove the GET attribute (so both GET and POST use the Details() Action) the problem goes away – but this also gets rid of my pretty URL’s when the form is submitted (and causes the annoying “are you sure?” popup on refresh). Strangely, the only problem is the loss of formatting in that field. Everything else is fine.

  • 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-18T09:44:48+00:00Added an answer on May 18, 2026 at 9:44 am

    While waiting for you to clearly specify the problem, here’s a full working counter example that what you describe in your question doesn’t actually happen:

    Model:

    public class MyViewModel
    {
        [DisplayName("As Configured On:")]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime ConfigDate { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                ConfigDate = DateTime.Now
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    View:

    <% using (Html.BeginForm()) { %>
        <%= Html.EditorFor(x => x.ConfigDate) %>
        <input type="submit" value="OK" />
    <% } %>
    

    You can submit the form as much as you wish, the formatting will be preserved.


    UPDATE:

    After providing additional information here’s why the problem occurs. When you redirect to the Details action with return RedirectToAction("Details", avm); a query string parameter is applied to the url:

    http://localhost:1114/Airplanes/Details?ConfigDate=11/30/2010%2000:00:00
    

    Notice how the hour is included and that’s normal. Now when you return the view in the Details GET action the HTML helper responsible for generating the editor template will do the following tasks:

    Check to see whether there’s a ConfigDate parameter (either GET or POST). If none was found check the value of the Model which is passed to the view and use the ConfigValue property of the model and generate the textbox.

    As a ConfigValue is found in the query string the model is not used at all. So it simply takes the value passed in the redirect which also contains the time and uses it to bind to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewModel which exposes the string property PageToolBarVisible which can be true
I have a simple ViewModel as given below [Bind(Exclude = State)] public class CityViewModel
In my ViewModel I have a property, which is a list of models: private
I have a textbox that i am binding to the viewmodel's string property. The
I have an int property in my viewmodel that returns a custom class property.
I have a boolean property in my ViewModel, named lets say IsNotSupported that is
I have a standard Listbox which is bound to a property in my viewmodel
ViewModel I have a property of type Member called KeyMember . The 'Member' type
I have two HeaderedContentControl s like those below that each have their content property
I have 2 properties in ViewModel class, EmployeeList and Employee. EmployeeList is dynamic property,

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.