Fix for Date and Int datatypes having new editors in MVC4

This commit is contained in:
2012-12-17 21:34:53 -05:00
parent f222cef2e5
commit 2bcca06b7e
12 changed files with 60 additions and 14 deletions
@@ -12,7 +12,7 @@ namespace Web.Tests.ViewModels
public void GetLog_Converts_To_Log() public void GetLog_Converts_To_Log()
{ {
var cityName = "My Town"; var cityName = "My Town";
var date = DateTime.Today.ToString("d"); var date = DateTime.Today;
var employeeName = "Name"; var employeeName = "Name";
var endOdometer = "1234"; var endOdometer = "1234";
var ethraId = "4567"; var ethraId = "4567";
+1 -1
View File
@@ -86,7 +86,7 @@ namespace MileageTraker.Web.Controllers
LogType = logType, LogType = logType,
EmployeeName = name, EmployeeName = name,
VehicleId = vehicleId, VehicleId = vehicleId,
Date = DateTime.Today.ToShortDateString() Date = DateTime.Today
}; };
} }
+2 -2
View File
@@ -26,12 +26,12 @@ namespace MileageTraker.Web.ViewModels
{ {
Mapper.Map(createLogViewModel, this); Mapper.Map(createLogViewModel, this);
DateHowLongAgo = (DateTime.Today - DateTime.Parse(Date)).ToVerboseStringHistoric(); DateHowLongAgo = (DateTime.Today - Date).ToVerboseStringHistoric();
var dataService = new DataService(); var dataService = new DataService();
var endOdometer = int.Parse(createLogViewModel.EndOdometer); var endOdometer = int.Parse(createLogViewModel.EndOdometer);
var date = DateTime.Parse(createLogViewModel.Date); var date = createLogViewModel.Date;
var previousLog = dataService.SearchPreviousLog(endOdometer, createLogViewModel.VehicleId, date, DateTime.Now); var previousLog = dataService.SearchPreviousLog(endOdometer, createLogViewModel.VehicleId, date, DateTime.Now);
if (previousLog != null) if (previousLog != null)
+3 -5
View File
@@ -54,14 +54,12 @@ namespace MileageTraker.Web.ViewModels
[Required(ErrorMessage = "Required")] [Required(ErrorMessage = "Required")]
[DataType(DataType.Date)] [DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] [DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[RegularExpression(@"([1-9]|0[1-9]|1[012])[- /.]?([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]?20\d{2}",
ErrorMessage = "Format is mm/dd/yyyy")]
[DenyFutureDate(ErrorMessage = "Future date")] [DenyFutureDate(ErrorMessage = "Future date")]
[DenyPreviousMonthDate(ErrorMessage = "Previous Month")] [DenyPreviousMonthDate(ErrorMessage = "Previous Month")]
[FormatHint("mm/dd/yyyy")] [FormatHint("mm/dd/yyyy")]
[InputSize("small")] [InputSize("small")]
public string Date { get; set; } public DateTime Date { get; set; }
static CreateLogViewModel() static CreateLogViewModel()
{ {
@@ -102,7 +100,7 @@ namespace MileageTraker.Web.ViewModels
try try
{ {
var dataService = new DataService(); var dataService = new DataService();
dataService.ValidateOdometerChronology(VehicleId, int.Parse(EndOdometer), DateTime.Parse(Date)); dataService.ValidateOdometerChronology(VehicleId, int.Parse(EndOdometer), Date);
} }
catch (Exception ex) catch (Exception ex)
{ {
+1 -1
View File
@@ -90,7 +90,7 @@
@Html.DisplayNameFor(m => m.Date) @Html.DisplayNameFor(m => m.Date)
</dt> </dt>
<dd> <dd>
@Html.DisplayTextFor(m => m.Date) @Html.Encode(Model.Date.ToShortDateString())
<small class="muted">(@Html.DisplayTextFor(m => m.DateHowLongAgo))</small> <small class="muted">(@Html.DisplayTextFor(m => m.DateHowLongAgo))</small>
</dd> </dd>
</dl> </dl>
+2 -2
View File
@@ -10,8 +10,8 @@
<p> <p>
You've successfully created an entry You've successfully created an entry
for <strong>@Html.DisplayTextFor(m => m.EmployeeName)</strong> for <strong>@Html.DisplayTextFor(m => m.EmployeeName)</strong>
traveling to <strong>@Html.DisplayTextFor(m => m.CityName)</strong> traveling to <strong>@Html.DisplayTextFor(m => m.CityName)</strong>
on <strong>@Html.DisplayTextFor(m => m.Date)</strong> on <strong>@Html.Encode(Model.Date.ToShortDateString())</strong>
in Vehicle Id <strong>@Html.DisplayTextFor(m => m.VehicleId)</strong> in Vehicle Id <strong>@Html.DisplayTextFor(m => m.VehicleId)</strong>
</p> </p>
<p> <p>
@@ -0,0 +1,5 @@
@model DateTime
@{
Layout = "~/Views/Shared/DisplayTemplates/_FieldLayout.cshtml";
}
@Html.Encode(string.Format(ViewData.ModelMetadata.DisplayFormatString, Model))
@@ -1,4 +1,11 @@
@{ @{
Layout = "~/Views/Shared/DisplayTemplates/_FieldLayout.cshtml"; Layout = "~/Views/Shared/DisplayTemplates/_FieldLayout.cshtml";
} }
@Html.Encode(Model) @if (Model == null)
{
@Html.Encode(@ViewData.ModelMetadata.NullDisplayText)
}
else
{
@Html.Encode(Model)
}
@@ -0,0 +1,13 @@
@model DateTime
@{
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
var inputSize = (string)ViewData.ModelMetadata.AdditionalValues["InputSize"];
}
@if (!string.IsNullOrEmpty(inputSize))
{
@Html.TextBox("", (Model != DateTime.MinValue ? Model.ToShortDateString() : string.Empty ), new { @class = inputSize})
}
else
{
@Html.TextBox("", (Model != DateTime.MinValue ? Model.ToShortDateString() : string.Empty ))
}
@@ -1,5 +1,13 @@
@model DateTime @model DateTime
@{ @{
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml"; Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
var inputSize = (string)ViewData.ModelMetadata.AdditionalValues["InputSize"];
}
@if (!string.IsNullOrEmpty(inputSize))
{
@Html.TextBox("", (Model != DateTime.MinValue ? Model.ToShortDateString() : string.Empty ), new { @class = inputSize})
}
else
{
@Html.TextBox("", (Model != DateTime.MinValue ? Model.ToShortDateString() : string.Empty ))
} }
@Html.TextBox("", (Model != DateTime.MinValue ? Model.ToShortDateString() : string.Empty ))
@@ -0,0 +1,12 @@
@{
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
var inputSize = (string)ViewData.ModelMetadata.AdditionalValues["InputSize"];
}
@if (!string.IsNullOrEmpty(inputSize))
{
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = inputSize})
}
else
{
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)
}
+3
View File
@@ -227,6 +227,9 @@
<Content Include="Views\Web.config" /> <Content Include="Views\Web.config" />
<Content Include="Views\Log\BackToLogs.cshtml" /> <Content Include="Views\Log\BackToLogs.cshtml" />
<Content Include="Views\Vehicle\BackToVehicles.cshtml" /> <Content Include="Views\Vehicle\BackToVehicles.cshtml" />
<Content Include="Views\Shared\EditorTemplates\Date.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\Date.cshtml" />
<Content Include="Views\Shared\EditorTemplates\Int32.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="packages.config" /> <Content Include="packages.config" />