Solved

Converting the web service DateTime string to the right format in MVC

Posted on 2016-09-09
1
44 Views
Last Modified: 2016-09-09
In my MVC Form

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate)                                        
                                    </div>

in my Model
         [DisplayName("Payment Date")]
        [DataType(DataType.Date)]
        public string achPayDate { get; set; }  

      (achPayDate -- is getting data from the web service)

my achPayDate =      2016-09-21T00:00:00-07:00  
and I wanted to format that to MM/dd/yyyy  so it is 09/21/2016.  

I tried

         [DisplayName("Payment Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString=("{0:MM/dd/yyyy}"))]
        public string achPayDate { get; set; }  

which is not working
and tried

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate, "{0:MM/dd/yyyy}")                                        
                                    </div>
and is not working

not sure what should I do
0
Comment
Question by:eli411
1 Comment
 
LVL 2

Accepted Solution

by:
eli411 earned 0 total points
ID: 41792080
Here is what I did and it solved my problem

Found the answer myself!  Here is what I did

in the Model

        [DisplayName("Payment Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? achPayDate { get; set; }

and in the MVC form

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate, new {@class="date"})                                        
                                    </div>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 32
Code enhancement 4 22
asp.net mvc , views, hidden values ? 2 16
Amazon S3 .Net error 5 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question