Files
MileageTraker/Web/ViewModels/VehicleService/VehicleServiceViewModel.cs
T

89 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
namespace MileageTraker.Web.ViewModels.VehicleService
{
public class VehicleServiceViewModel : IValidatableObject
{
[HiddenInput(DisplayValue = false)]
public int? VehicleServiceId { get; set; }
[HiddenInput(DisplayValue = true)]
public string VehicleId { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
[DenyFutureDate(ErrorMessage = "Future date")]
[FormatHint("mm/dd/yyyy")]
[InputSize("small")]
[Required]
public DateTime? InvoiceDate { get; set; }
[Required]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
public string ServiceCenterName { get; set; }
[Required]
[StringLength(32)]
[InputSize("small")]
public string InvoiceNumber { get; set; }
[Required]
[InputSize("small")]
[Currency]
public decimal Price { get; set; }
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
public string Description { get; set; }
[Display(Name = "Vehicle Recall")]
[OptionLabel("Not a recall")]
[UIHint("VehicleRecallSelectListViewModel")]
public SelectListViewModel VehicleRecall { get; set; }
static VehicleServiceViewModel()
{
Mapper.CreateMap<VehicleServiceViewModel, Models.VehicleService>();
Mapper.CreateMap<Models.VehicleService, VehicleServiceViewModel>()
.ForMember(dest => dest.VehicleId, opt => opt.MapFrom(src => src.Vehicle.VehicleId));
}
public VehicleServiceViewModel(Models.VehicleService vehicleService) : this()
{
Mapper.Map(vehicleService, this);
}
public VehicleServiceViewModel()
{
//var enumerable = new string[]{};
//VehicleRecallId = new SelectListViewModel { Available = new SelectList(enumerable, "VehicleRecallId", "RecallId") };
}
public Models.VehicleService GetVehicleService()
{
var vehicleService = new Models.VehicleService();
Mapper.Map(this, vehicleService);
return vehicleService;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
using (var dataService = new DataService())
{
var recallId = VehicleRecall?.Selected ?? int.MinValue;
// if there's a recall, verify it's for this vehicle
if (recallId >= 0 && dataService.GetVehicleRecall(recallId).Vehicle.VehicleId != VehicleId)
{
yield return new ValidationResult("This recall is not for the given vehicle",
new[] {"VehicleRecall"});
}
}
}
}
}