7 Replies - 1670 Views - Last Post: 27 July 2012 - 05:38 PM

#1 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Substring : razor/HTML or Entity Framework Issue?

Posted 26 July 2012 - 11:44 AM

Hi all,

I am currently returning the results of a query to an MVC view, and having a little trouble parsing the "date" (format: MM-dd-YYYY) out of the datetime datatype (format: MM-dd-YYYY hh-mm-ss) returned by my query. I was wondering if it was possible to strip off the date portion with some HTML/razor code or if I should be handling this inside my linq to entities query?

The view code is below:
@foreach(var pastSession in ViewData.Model)
            {
                <tr>
                    <td>@pastSession.Date</td>...

The query that is being returned to the controller:
var query = (from s in db.Sessions
                             join tuteeStudent in db.Students on s.TuteeStudentID equals tuteeStudent.StudentID
                             join tutorStudent in db.Students on s.TutorStudentID equals tutorStudent.StudentID
                             join c in db.Courses on s.CourseID equals c.CourseID
                             join p in db.Professors on c.ProfessorID equals p.ProfessorID
                             orderby s.Date
                             select new PastSession { Date = s.Date, Time = s.Time, Tutee = tuteeStudent.FullName, CourseNumber = c.CourseNumber, CourseName = c.CourseName, Professor = p.ProfessorLastName, Tutor = tutorStudent.FullName }).ToList();


Any advice on how to solve this problem would be much appreciated, as I don't really know which area (HTML, Razor or EF) can handle this problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Substring : razor/HTML or Entity Framework Issue?

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 26 July 2012 - 11:53 AM

What do you mean by this?

Do you have a DateTime field in your database? If so, why is there a separate s.Date and s.Time? Also, what is being output in the HTML at the moment?

You can always call .ToString("MM-dd-yyyy") in your Razor code, this is a presentation issue not a data issue, so it makes more sense there.
Was This Post Helpful? 0
  • +
  • -

#3 mvcwannab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-July 12

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 26 July 2012 - 02:57 PM

The easiest way to do this is to put the [DisplayFormat] annotation above the field in your model for the field you want the time stripped from.

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateToTrim { get;set; }


Was This Post Helpful? 0
  • +
  • -

#4 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 27 July 2012 - 04:29 AM

I did know about the DisplayFormat option, but since this result type was not in a MVC web project, but a class file in a class library project I did not know if this was best practice or not. I have added the reference to System.ComponentModel.DataAnnotations to my class library file (in the same solution as MVC project) and added the line you suggested:

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]


However, I am still getting the same results as before: the date with the hours, minutes and seconds. Also, can you explain what the
 ApplyFormatInEditMode = true)
does? I tried looking this up on MSDN and got a pretty convoluted answer.

Quote

You can always call .ToString("MM-dd-yyyy") in your Razor code, this is a presentation issue not a data issue, so it makes more sense there.


Rudi, can I add the .ToString() method inside of the view code? and how do I do this? I was expecting the equivalent to:
 <td>@pastSession.Date.ToString()</td>
but this is obviously not correct.

To answer your other questions: the date is in this current format: 1/23/2012 12:00:00 AM, in the database this field is of type date, and the s.Time field is of type time. The query I posted above is returning PastSessions, the definition is located here:

    public class PastSession
    {
        [DisplayFormat(DataFormatString="0:d", ApplyFormatInEditMode=true)]
        public DateTime Date { get; set; }

        public TimeSpan Time { get; set; }
        public String Tutee { get; set; }
        public String CourseNumber { get; set; }
        public String CourseName { get; set; }
        public String Professor { get; set; }
        public String Tutor { get; set; }
    }


Please let me know if I can answer anything else. Your responses have been helpful.
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 27 July 2012 - 05:51 AM

Ah so Time is a duration rather than an actual time, that makes sense.

What I am suggesting is this:
<td>@pastSession.Date.ToString("dd/MM/yyyy")</td>


That should work fine (I'm actually using it in an email template I have running through Razor here).
Was This Post Helpful? 1
  • +
  • -

#6 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 27 July 2012 - 06:44 AM

Thanks a lot! And I did not mean for time to be a duration--In the database it is type time (as in time of day), but I did not see a type time in C#. I more or less made it of type TimeSpan to make visual studio happy. I suppose I could make it a datetime in C# and use your .ToString() method in razor to format it.

As you can tell I am a total newbie.
Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 27 July 2012 - 11:13 AM

View PostItIntern3, on 27 July 2012 - 02:44 PM, said:

Thanks a lot! And I did not mean for time to be a duration--In the database it is type time (as in time of day), but I did not see a type time in C#. I more or less made it of type TimeSpan to make visual studio happy. I suppose I could make it a datetime in C# and use your .ToString() method in razor to format it.

Well DateTime is exactly that, a Date and a Time. You should just insert a DateTime.Now value (generated from the database of course) into that field and then you can use the DateTime formats to output the value you need.

A TimeSpan won't actually store a time, just a duration, hence it's name. I have no idea how you're getting that to work :) Unless you're like, storing a 19 hour and 26 minute and 30 second duration for 19:26:30, but that would make me sad! :D :D
Was This Post Helpful? 0
  • +
  • -

#8 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Re: Substring : razor/HTML or Entity Framework Issue?

Posted 27 July 2012 - 05:38 PM

Quote

Well DateTime is exactly that, a Date and a Time. You should just insert a DateTime.Now value (generated from the database of course) into that field and then you can use the DateTime formats to output the value you need.


Alright I can certainly do that. I'll just have to alter the column in my SQL database to be datetime rather than time. I did not know if razor's ability to format data inside the view when I was creating my db. Thanks for your advice.

Again, I am still baffled by this ApplyInEditMode = true statement (namespace: System.ComponentModel.DataAnnotations):
[DisplayFormat(DataFormatString="0:d", ApplyFormatInEditMode=true)]
public DateTime Date { get; set; }



I understand that the DataAnnotations class is used to validate data that comes from the user on the view, but I haven't the slightest clue what this does...Can anyone give me a concise/understandable synopsis of the msdn page?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1