3

I am novice to the asp.net mvc3. It's really confusing and difficult to modify single code due to convention used in asp.net mvc3. I was trying to display only Date for BirthDate in the format 5 Sep 1999 instead which shows Date and Time.It's fine in Edit.cshtml, datepicker is used to pick the date and value is saved in database of only date. But, I have BirthDate column of Data type of Date not the DateTime and when using @Html.DisplayFor(model => model.BirthDate); in Details.cshtml shows both date and time. While Searching in google I have found and implement following code for displaying date in desire format:

@Model.BirthDate.ToString("dd MMM YYYY");

and

@Html.DisplayFor(model => model.BirthDate.ToString("dd MMM YYYY"));

It gives error no overload method takes 1 argument. Further I could use like:

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
pubilc DateTime BirthDate { get; set }

Since, I have used model first approach for Entity Framework. Where Should I implement above DisplayFormat property or what may be razor syntax to display date in right way in Details.csthml in my scenario

4 Answers 4

1

ok, I know I'm replying to a question posted 8 month before but my only intention is, this might be useful to others refering this question in future.
I'm also novoice to MVC and I also faced a similar problem where I need to display only the date and not the time and following worked for me.

Instead of @Html.DisplayFor(). Use <span>
So, for the case mentioned in question it would be like this:

<span>@String.Format("{0:D}", model.BirthDate)</span>

Output: Sunday, September 05, 1999 

No, need to add extra class/file for formating.

Sign up to request clarification or add additional context in comments.

Comments

0

The third approach is the best way according to me cos

  1. your presentation model is dealing with all the aspects of UI and your view doesnt have unnecessary and redundant formatting code especially if you reuse the property.
  2. enables unit testing
  3. Consistent across different pages if you reuse the model.

You could also write an helper method that formats the date and use this consistently across all your presentation/view models.

public string FormattedDate(this DateTime dateTime)
{
  return dateTime.ToString("dd MMM YYYY");
}

2 Comments

Thanks for response. I am totally new to mvc3 would you plz give me steps how do I implement helper in my scenario
just create a class called ViewFormatter/FormatHelper put in the methods that you need and then refer to this namespace within your View or Model
0

I like using a kind of decorator pattern to handle this kind of thing. Let's say your model class is called MyModel. Then define a wrapper class like this:

public class MyModelDecorator 
{
    public MyModel BaseItem { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime BirthDate { get; set; }

    public MyModelDecorator(MyModel baseItem)
    {
        BaseItem = baseItem;
    }
}

Then in your Views, you can refer to either the base model properties, or to the decorated properties.

@Html.DisplayFor(m => m.BirthDate)
@Html.DisplayFor(m => m.BaseItem.SomeOtherProperty)

If there's a better solution than this one, then I'd really like to hear it....

4 Comments

ok, can't get how to use. I have created folder Decorator and MyModelDecorator.cs inside it. Now, in view I have reference as @model IVRControlPanel.Decorator.MyModelDecorator. The property of MyModel is not binding but BirthDate property is detected. Or, I am doing wrong
@CodeManiac MyModel isn't the property name in this example, it's BaseItem. Can name it whatever you like though.
I am not saying it property name. Here In my scenario I have user class (as MyModel) and have property like username, email, password, etc. Which are not binded to my view
@CodeManiac right ... you have to use MyModel.BaseItem.username, etc. If you want to access them as MyModel.username, then you have to add them to the decorator class.
0

For any ASPX user sumbad answer helped but this is how you do it:

<%=Html.Enconde(String.Format("{0:D}", item.yourDate))%>

Hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.