diff --git a/Core/DAL/DataService.cs b/Core/DAL/DataService.cs index 42f3e3c..45a1252 100644 --- a/Core/DAL/DataService.cs +++ b/Core/DAL/DataService.cs @@ -60,5 +60,15 @@ namespace LeafWeb.Core.DAL } #endregion + + public IQueryable GetPhotosynthesisTypes() + { + return _db.PhotosynthesisTypes.OrderBy(pt => pt.SortOrder); + } + + public PhotosynthesisType GetPhotosynthesisType(string id) + { + return _db.PhotosynthesisTypes.Find(id); + } } } diff --git a/Core/DAL/LeafWebInitializer.cs b/Core/DAL/LeafWebInitializer.cs index c0a66b8..250f0b7 100644 --- a/Core/DAL/LeafWebInitializer.cs +++ b/Core/DAL/LeafWebInitializer.cs @@ -22,9 +22,9 @@ namespace LeafWeb.Core.DAL var photosynthesisTypes = new [] { - new PhotosynthesisType {Id = "C3_photosynthesis_leafweb", Name = "C3 Photosynthesis"}, - new PhotosynthesisType {Id = "C4_photosynthesis_leafweb", Name = "C4 Photosynthesis"}, - new PhotosynthesisType {Id = "CAM_photosynthesis_leafweb", Name = "CAM Photosynthesis"} + new PhotosynthesisType {Id = "C3_photosynthesis_leafweb", Name = "C3 Photosynthesis", SortOrder = 1}, + new PhotosynthesisType {Id = "C4_photosynthesis_leafweb", Name = "C4 Photosynthesis", SortOrder = 2}, + new PhotosynthesisType {Id = "CAM_photosynthesis_leafweb", Name = "CAM Photosynthesis", SortOrder = 3} }; context.PhotosynthesisTypes.AddRange(photosynthesisTypes); diff --git a/Core/Models/LeafInput.cs b/Core/Models/LeafInput.cs index 5fc672a..b59caa9 100644 --- a/Core/Models/LeafInput.cs +++ b/Core/Models/LeafInput.cs @@ -23,7 +23,8 @@ namespace LeafWeb.Core.Models [Required(ErrorMessage = "")] public string SiteId { get; set; } - public virtual PhotosynthesisType Photosynthesis { get; set; } + [Required(ErrorMessage = "PhotosynthesisType required")] + public virtual PhotosynthesisType PhotosynthesisType { get; set; } [DataType(DataType.Date)] [Required] diff --git a/Core/Models/PhotosynthesisType.cs b/Core/Models/PhotosynthesisType.cs index 2d16b09..6c14891 100644 --- a/Core/Models/PhotosynthesisType.cs +++ b/Core/Models/PhotosynthesisType.cs @@ -10,5 +10,7 @@ namespace LeafWeb.Core.Models public string Id { get; set; } public string Name { get; set; } + + public int SortOrder { get; set; } } } diff --git a/Web/Controllers/ControllerBase.cs b/Web/Controllers/ControllerBase.cs index 70d19c4..3d6de5f 100644 --- a/Web/Controllers/ControllerBase.cs +++ b/Web/Controllers/ControllerBase.cs @@ -44,5 +44,10 @@ namespace LeafWeb.Web.Controllers break; } } + + protected SelectList GetPhotosynthesisTypeSelectList() + { + return new SelectList(DataService.GetPhotosynthesisTypes().ToList(), "Id", "Name"); + } } } \ No newline at end of file diff --git a/Web/Controllers/LeafInputController.cs b/Web/Controllers/LeafInputController.cs index efe18f3..a913369 100644 --- a/Web/Controllers/LeafInputController.cs +++ b/Web/Controllers/LeafInputController.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Mvc; using LeafWeb.Core.Models; using LeafWeb.Web.Attributes; +using LeafWeb.Web.ViewModels; using LeafWeb.Web.ViewModels.LeafInput; namespace LeafWeb.Web.Controllers @@ -15,25 +16,9 @@ namespace LeafWeb.Web.Controllers { // initialize the session storage to retain SessionID between requests Session["placeholder"] = 0; - return View(); - } - - private FileInfo[] GetBackloadDirectoryFiles(string directoryName) - { - var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); - var directory = new DirectoryInfo(path); - return - !directory.Exists - ? new FileInfo[] {} - : directory.GetFiles(); - } - - private void DeleteBackloadDirectory(string directoryName) - { - var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); - var directory = new DirectoryInfo(path); - if (directory.Exists) - directory.Delete(true); + var viewModel = new CreateViewModel(); + HydrateCreateViewModel(viewModel); + return View(viewModel); } [HttpParamAction] @@ -50,9 +35,21 @@ namespace LeafWeb.Web.Controllers { // Go to confirmation var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray()); + HydrateCreateViewModel(confirmViewModel); + return View("Confirm", confirmViewModel); } - return View("Index"); + + HydrateCreateViewModel(viewModel); + return View("Index", viewModel); + } + + private void HydrateCreateViewModel(dynamic viewModel) + { + if (viewModel.PhotosynthesisType == null) + viewModel.PhotosynthesisType = new SelectListViewModel(); + if (viewModel.PhotosynthesisType.ListItems == null) + viewModel.PhotosynthesisType.ListItems = GetPhotosynthesisTypeSelectList(); } [HttpParamAction] @@ -70,7 +67,7 @@ namespace LeafWeb.Web.Controllers if (ModelState.IsValid) { // convert viewModel into Model - var leafInput = viewModel.GetFileInput(); + var leafInput = viewModel.GetFileInput(DataService); // load files into LeafInputFile leafInput.LeafInputFiles = (from f in files @@ -89,7 +86,27 @@ namespace LeafWeb.Web.Controllers StatusType.Success); return RedirectToAction("Index"); } + + HydrateCreateViewModel(viewModel); return View("Index", viewModel); } + + private FileInfo[] GetBackloadDirectoryFiles(string directoryName) + { + var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); + var directory = new DirectoryInfo(path); + return + !directory.Exists + ? new FileInfo[] { } + : directory.GetFiles(); + } + + private void DeleteBackloadDirectory(string directoryName) + { + var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); + var directory = new DirectoryInfo(path); + if (directory.Exists) + directory.Delete(true); + } } } \ No newline at end of file diff --git a/Web/ViewModels/LeafInput/ConfirmViewModel.cs b/Web/ViewModels/LeafInput/ConfirmViewModel.cs index a028ec9..6b16aa6 100644 --- a/Web/ViewModels/LeafInput/ConfirmViewModel.cs +++ b/Web/ViewModels/LeafInput/ConfirmViewModel.cs @@ -7,10 +7,16 @@ namespace LeafWeb.Web.ViewModels.LeafInput { private static readonly IMapper Mapper; + [Display(Name = "Your name")] public string Name { get; set; } + [Display(Name = "Your email")] public string Email { get; set; } + [Display(Name = "Data identifier")] public string Identifier { get; set; } + [Display(Name = "Site Id")] public string SiteId { get; set; } + [Display(Name = "Photosynthetic Pathway")] + public SelectListViewModel PhotosynthesisType { get; set; } public string[] Files { get; set; } static ConfirmViewModel() diff --git a/Web/ViewModels/LeafInput/CreateViewModel.cs b/Web/ViewModels/LeafInput/CreateViewModel.cs index 9e0a6c5..5c29ad0 100644 --- a/Web/ViewModels/LeafInput/CreateViewModel.cs +++ b/Web/ViewModels/LeafInput/CreateViewModel.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using AutoMapper; +using LeafWeb.Core.DAL; namespace LeafWeb.Web.ViewModels.LeafInput { @@ -31,21 +32,28 @@ namespace LeafWeb.Web.ViewModels.LeafInput [Required(ErrorMessage = "The site's name is required")] public string SiteId { get; set; } + [Display(Name = "Photosynthetic Pathways")] + [Required(ErrorMessage = "A photosynthesis pathway must be chosen")] + public SelectListViewModel PhotosynthesisType { get; set; } static CreateViewModel() { var config = new MapperConfiguration(cfg => { - cfg.CreateMap(); + cfg.CreateMap() + .ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore()); }); Mapper = config.CreateMapper(); } - public Core.Models.LeafInput GetFileInput() + public Core.Models.LeafInput GetFileInput(DataService db) { var leafInput = new Core.Models.LeafInput(); Mapper.Map(this, leafInput); + + leafInput.PhotosynthesisType = db.GetPhotosynthesisType(PhotosynthesisType.Selected); + return leafInput; } } diff --git a/Web/ViewModels/SelectListViewModel.cs b/Web/ViewModels/SelectListViewModel.cs new file mode 100644 index 0000000..2ce0ca8 --- /dev/null +++ b/Web/ViewModels/SelectListViewModel.cs @@ -0,0 +1,21 @@ +using System.Linq; +using System.Web.Mvc; + +namespace LeafWeb.Web.ViewModels +{ + public class SelectListViewModel + { + public SelectList ListItems { get; set; } + public string Selected { get; set; } + + public SelectListItem SelectedItem + { + get { return ListItems.FirstOrDefault(li => li.Value == Selected); } + } + + public override string ToString() + { + return Selected; + } + } +} \ No newline at end of file diff --git a/Web/Views/LeafInput/Confirm.cshtml b/Web/Views/LeafInput/Confirm.cshtml index 186cd59..bebafa0 100644 --- a/Web/Views/LeafInput/Confirm.cshtml +++ b/Web/Views/LeafInput/Confirm.cshtml @@ -48,6 +48,16 @@ @Html.HiddenFor(m => m.SiteId) +
+
+ @Html.DisplayNameFor(m => m.PhotosynthesisType) +
+
+ @Html.DisplayTextFor(m => m.PhotosynthesisType.SelectedItem.Text) +
+
+ +
Files diff --git a/Web/Views/LeafInput/Index.cshtml b/Web/Views/LeafInput/Index.cshtml index 4341a1a..3531ba6 100644 --- a/Web/Views/LeafInput/Index.cshtml +++ b/Web/Views/LeafInput/Index.cshtml @@ -21,6 +21,7 @@ @Html.EditorFor(m => m.EmailConfirm) @Html.EditorFor(m => m.Identifier) @Html.EditorFor(m => m.SiteId) + @Html.EditorFor(m => m.PhotosynthesisType) } diff --git a/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml b/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml new file mode 100644 index 0000000..e3c4d58 --- /dev/null +++ b/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml @@ -0,0 +1,18 @@ +@model LeafWeb.Web.ViewModels.SelectListViewModel +@{ + Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml"; +} +@{ + var prefix = ViewData.TemplateInfo.HtmlFieldPrefix; + ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty; + + foreach (var li in Model.ListItems) + { +
+ +
+ } + ViewData.TemplateInfo.HtmlFieldPrefix = prefix; +} diff --git a/Web/Views/Shared/EditorTemplates/_FieldLayout.cshtml b/Web/Views/Shared/EditorTemplates/_FieldLayout.cshtml index ba5ecb2..2919127 100644 --- a/Web/Views/Shared/EditorTemplates/_FieldLayout.cshtml +++ b/Web/Views/Shared/EditorTemplates/_FieldLayout.cshtml @@ -2,14 +2,17 @@ @model object @{ Layout = null; - var lowerPropertyName = @LeafWeb.Core.Utility.StringExtensions.LowercaseFirst(ViewData.ModelMetadata.PropertyName); + var propertyName = ViewData.ModelMetadata.PropertyName; + var lowerPropertyName = @LeafWeb.Core.Utility.StringExtensions.LowercaseFirst(propertyName); var values = ViewData.ModelMetadata.AdditionalValues; var units = values.ContainsKey("Units") ? (string)values["Units"] : null; var currency = values.ContainsKey("Currency") ? (string)values["Currency"] : null; var formatHint = values.ContainsKey("FormatHint") ? (string)values["FormatHint"] : null; var editLabel = values.ContainsKey("EditLabel") ? (bool)values["EditLabel"] : true; + var hasError = ViewData.ModelState[propertyName] != null && ViewData.ModelState[propertyName].Errors.Any(); + var hasErrorClass = hasError ? "has-error" : string.Empty; } -
+
@Html.LabelForModel(new { @class = "control-label" }) @RenderBody() @Html.ValidationMessage("", new { @class = "help-block"}) diff --git a/Web/Web.csproj b/Web/Web.csproj index 1401284..ee327ac 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -905,6 +905,7 @@ + @@ -940,6 +941,7 @@ + Web.config