-4

I'm using Mvc 4. The following is my code.

@Html.Bootstrap().ControlGroup().DisplayTextFor(m => m.BirthdayDate).Label().LabelText(Person.Resources.Global.BirthDay);

It's display the data with date and time. I want to display only date. How to do that ?

Already I've tried with BirthDay.value.ToShortDateString() and BirthDay.ToString("dd/mm/yyyy"). But this gives error, since BirthDay is Nullable DateTime

I got the following error if i use BirthDay.value.ToShortDateString() and BirthDay.ToString("dd/mm/yyyy") and someDate.Value.ToShortDateString()

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Jeeva J
  • 3,173
  • 10
  • 38
  • 85

3 Answers3

1

Only model value can be add like these. So try to not null-able value to modal and try to use as your wish. That could be helpful for you.

0

If the date is nullable you better use

someDate.Value.ToShortDateString();

even better:

someDate.GetValueOrDefault().ToShortDateString();

of if you do not want return any date if someDate is null then you shall do the following:

if(someDate.HasValue)
{
    someDate.Value.ToShortDateString();
}
Michael Mairegger
  • 6,833
  • 28
  • 41
  • I've already mentioned nullable date time. I cant change for Nullable DateTime. – Jeeva J Nov 14 '13 at 10:33
  • I do not understand your comment. You mentioned Nullable DateTime. All of my code works for Nullable DateTime – Michael Mairegger Nov 14 '13 at 11:23
  • Conversion to specific datetime format for nullable dateTime object (for example dateTimeObj.toString("dd/mm/yyyy") or like you all specified above) is not working. – Jeeva J Nov 14 '13 at 11:35
  • If AOCrowdFund.Resources.Global.BirthDay is of type `DateTime?` then it would. Otherwise it is not of that type. – Michael Mairegger Nov 14 '13 at 11:36
  • That's only to display text not any data. m => m.BirthdayDate this is of type `DateTime?` – Jeeva J Nov 14 '13 at 12:00
  • Then why don't you appy my suggestion on m.BirthdayDate? – Michael Mairegger Nov 14 '13 at 12:06
  • How long I should repeat my same reply to you @xxMUROxx. If i use your code, I got the following error. `Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.` – Jeeva J Nov 14 '13 at 12:18
  • I try to help and understand the error. Therefore, this should also be explained. Also, the question is not written clearly. – Michael Mairegger Nov 14 '13 at 12:24
0

Sorry I forget to mention. I am using https://www.twitterbootstrapmvc.com/

If the object is nullable, then

For plain html helper, use like following,

label -

 @Html.DisplayFor(t => t.YourDateObject, "{0:MM/dd/yyyy}") 

Textbox - @Html.TextBoxFor(t => t.YourDateObject, "{0:MM/dd/yyyy}")

For https://www.twitterbootstrapmvc.com/, then use like following,

@Html.Bootstrap().ControlGroup().DisplayTextFor(m=>m.BirthdayDate).Format("0:dd/MM/yyyy}").Label().LabelText(Person.Resources.Global.BirthDay)

I got reference from the following link. Formatted Date TextBoxFor

Community
  • 1
  • 1
Jeeva J
  • 3,173
  • 10
  • 38
  • 85