@Html.LabelFor(mod...">

Convert DateTime string of web service to MM / dd / yyyy format in MVC application

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 retrieves data from a web service

achPayDate = 2016-09-21T00:00:00-07:00 

And I wanted to format this for MM / dd / yyyy, so this is 09/21/2016. I tried

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

which does not work. I also 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>

which does not work

Not sure what to do

+4
source share
1 answer

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 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>
+1
source

Source: https://habr.com/ru/post/1654184/


All Articles