I have the following problem that I don’t know how fix:
I cannot display a time field correctly, with a template that I’m using @Html.DisplayFor
I can display correctly when I use @Html.TextBox, but I don’t want to display in textbox.
Please help.
Here is the code:
Details.cshtml
<div style="float: left; margin-right: 10px; color: #0000FF;">
<br />
<br />
<br />
@Html.LabelFor(model => model.Mon_Start_time) <br />
@Html.DisplayFor(model => model.Mon_Start_time)
@Html.ValidationMessageFor(model => model.Mon_Start_time)
</div>
<div style="float: left; margin-right: 10px; color: #0000FF;">
<br />
<br />
<br />
@Html.LabelFor(model => model.Mon_End_time) <br />
@Html.DisplayFor(model => model.Mon_End_time)
@Html.ValidationMessageFor(model => model.Mon_End_time)
</div>
Time.cshtml
@model Nullable<DateTime>
@{
DateTime dt = DateTime.Now;
if (Model != null)
{
dt = (System.DateTime) Model;
}
If I use this TextBox I got the time displayed but I do not want to display in textbox
@Html.TextBox("", String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })
If I use this code the time does not display at all
@Html.DisplayFor((model => model), String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })
}
I'm sorry that did not explain this more correctly. My entries are going to be for Monday through Frinday with starting and ending time eg: Mon_Start_time, Mon_End_time, Tue_start_time, Tue_End_time, etc, etc.
I know that this "@Html.DisplayFor((model => model), String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })" is not correct and that your suggestion I already coded before posting my problem. It does not work
anyways when I do that it gives me an error message. But I don’t wan to code for every entry, it has to be something more general like when I do it for @Html.TextBox where I don not specify the field name.
You need to do the following
<span>
@Html.DisplayFor((modelItem => item.date.Value.Hour))
</span>:
<span>
@Html.DisplayFor((modelItem => item.date.Value.Minute))
</span>
mozart_azul
Member
4 Points
25 Posts
@Html.DisplayFor not working
Apr 23, 2012 03:23 PM|LINK
Hello,
I have the following problem that I don’t know how fix:
I cannot display a time field correctly, with a template that I’m using @Html.DisplayFor
I can display correctly when I use @Html.TextBox, but I don’t want to display in textbox.
Please help.
Here is the code:
Details.cshtml <div style="float: left; margin-right: 10px; color: #0000FF;"> <br /> <br /> <br /> @Html.LabelFor(model => model.Mon_Start_time) <br /> @Html.DisplayFor(model => model.Mon_Start_time) @Html.ValidationMessageFor(model => model.Mon_Start_time) </div> <div style="float: left; margin-right: 10px; color: #0000FF;"> <br /> <br /> <br /> @Html.LabelFor(model => model.Mon_End_time) <br /> @Html.DisplayFor(model => model.Mon_End_time) @Html.ValidationMessageFor(model => model.Mon_End_time) </div> Time.cshtml @model Nullable<DateTime> @{ DateTime dt = DateTime.Now; if (Model != null) { dt = (System.DateTime) Model; } If I use this TextBox I got the time displayed but I do not want to display in textbox @Html.TextBox("", String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" }) If I use this code the time does not display at all @Html.DisplayFor((model => model), String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" }) }tusharrs
Contributor
3230 Points
668 Posts
Re: @Html.DisplayFor not working
Apr 23, 2012 03:31 PM|LINK
Hi,
refer to this link
http://buildstarted.com/2010/09/29/htmlhelper-guide-for-mvc-3-part-2/
( Mark as Answer if it helps you out )
View my Blog
mozart_azul
Member
4 Points
25 Posts
Re: @Html.DisplayFor not working
Apr 23, 2012 03:46 PM|LINK
I'm sorry that did not explain this more correctly. My entries are going to be for Monday through Frinday with starting and ending time eg: Mon_Start_time, Mon_End_time, Tue_start_time, Tue_End_time, etc, etc.
I know that this "@Html.DisplayFor((model => model), String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })" is not correct and that your suggestion I already coded before posting my problem. It does not work anyways when I do that it gives me an error message. But I don’t wan to code for every entry, it has to be something more general like when I do it for @Html.TextBox where I don not specify the field name.
tusharrs
Contributor
3230 Points
668 Posts
Re: @Html.DisplayFor not working
Apr 23, 2012 03:59 PM|LINK
hi,
@item.Date.ToString("HH:mm")
or you could use the [DisplayFormat] attribute on your view model:
[DisplayFormat(DataFormatString = "{0:HH:mm}")]
pubilc DateTime Date { get; set }
______________________________________________________________________________________
and in your view simply:
@Html.DisplayFor(x => x.Date)
( Mark as Answer if it helps you out )
View my Blog
mozart_azul
Member
4 Points
25 Posts
Re: @Html.DisplayFor not working
Apr 23, 2012 04:21 PM|LINK
By doing this:
@Html.DisplayFor((model => model.Value.TimeOfDay), String.Format("{0:HH}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })I getting the time but with the incorrect format.
I need to get only the HH:mm and not HH:MM:SS. Obviously the code “String.Format("{0:HH}" is getting ignored.
Please advice.
tusharrs
Contributor
3230 Points
668 Posts
Re: @Html.DisplayFor not working
Apr 24, 2012 06:04 AM|LINK
hi,
try this
this:
@Html.DisplayFor((model => model.Value.TimeOfDay), String.Format("{0:HH:mm}", dt.ToShortTimeString()))( Mark as Answer if it helps you out )
View my Blog
mozart_azul
Member
4 Points
25 Posts
Re: @Html.DisplayFor not working
Apr 24, 2012 12:49 PM|LINK
tusharrs,
Thanks for your response.
Unfortunately it does not work.
I have tried your suggestion
@Html.DisplayFor((model => model.Value.TimeOfDay), String.Format("{0:HH:mm}", dt.ToShortTimeString()))and also the one below (which is basically the same that I use for @htm.TextBox), without any positive result
@Html.DisplayFor((model => model.Value.TimeOfDay), String.Format("{0:HH:mm}", dt.ToShortTimeString()), new { @class = "datefield", type = "time" })I do not understand why the formatting is ignored.
legaleagle
Member
2 Points
1 Post
Re: @Html.DisplayFor not working
Apr 25, 2012 11:40 AM|LINK
You need to do the following <span> @Html.DisplayFor((modelItem => item.date.Value.Hour)) </span>: <span> @Html.DisplayFor((modelItem => item.date.Value.Minute)) </span>mozart_azul
Member
4 Points
25 Posts
Re: @Html.DisplayFor not working
Apr 25, 2012 03:03 PM|LINK
legaleagle,
Thanks for your response.
The problem that I am having now is the format.
Starting Ending Worked Hours Indirect Hours
Monday : 3/26/2012 8 :0 16 :0 8 0
3/27/2012 9 :0 16 :0 7 1
3/28/2012 10 :0 16 :0 6 2
3/29/2012 8 :0 16 :0 5 3
3/30/2012 8 :0 16 :0 4 4
If you noticed, the Hour format is coming "H" or sometimes "HH" and the minutes are always "m"
I need to format to "HH" for the Hour and "mm" for the minutes.
mozart_azul
Member
4 Points
25 Posts
Re: @Html.DisplayFor not working
Apr 26, 2012 04:36 PM|LINK
I figured out.
@String.Format("{0:HH:mm}", Model)
Thanks for your responses anyway