Add index and create

This commit is contained in:
2014-05-20 14:24:08 -04:00
parent ef88c52658
commit 0fcfa3a3a1
8 changed files with 159 additions and 22 deletions
+59 -1
View File
@@ -1,13 +1,71 @@
using System.Web.Mvc; using System;
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
namespace MileageTraker.Web.Controllers namespace MileageTraker.Web.Controllers
{ {
[Authorize(Roles = "Administrator, Developer")]
public class CityController : ControllerBase public class CityController : ControllerBase
{ {
[Authorize(Roles = "Administrator, Developer, Driver")]
public JsonResult Autocomplete(string term) public JsonResult Autocomplete(string term)
{ {
var cities = DataService.GetCitiesAutocomplete(term); var cities = DataService.GetCitiesAutocomplete(term);
return Json(cities, JsonRequestBehavior.AllowGet); return Json(cities, JsonRequestBehavior.AllowGet);
} }
public ActionResult Index()
{
var cities = DataService.GetCities().ToList();
var t = cities.Select(c => Tuple.Create(c.County, c.Name));
var groupTuple = CustomExtensions.GroupTuple(t);
return View(groupTuple);
}
public ActionResult Create(string county)
{
var city = new City { County = county };
return View(city);
}
//[HttpPost]
//public ActionResult Create([Bind(Exclude = "CityId")]City city)
//{
// if (ModelState.IsValid)
// {
// DataService.AddCity(city);
// TempData["StatusMessage"] = "City " + city.Name + " created";
// return RedirectToAction("Index");
// }
// return View(city);
//}
//public ActionResult Edit(int id = 0)
//{
// var city = DataService.FindCity(id);
// if (city == null)
// {
// TempData["StatusMessage"] = "Purpose " + id + " not found";
// return RedirectToAction("Index");
// }
// return View(city);
//}
//[HttpPost]
//public ActionResult Edit(City city)
//{
// if (ModelState.IsValid)
// {
// DataService.UpdateCity(city);
// TempData["StatusMessage"] = "Changes to Purpose " + city.Purpose + " saved";
// return RedirectToAction("Index");
// }
// return View(city);
//}
} }
} }
+1 -1
View File
@@ -487,7 +487,7 @@ namespace MileageTraker.Web.DAL
select city.Name; select city.Name;
} }
private IQueryable<City> GetCities() public IQueryable<City> GetCities()
{ {
return _db.Cities; return _db.Cities;
} }
+7
View File
@@ -1,12 +1,19 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
namespace MileageTraker.Web.Models namespace MileageTraker.Web.Models
{ {
public class City public class City
{ {
[Key] [Key]
[HiddenInput(DisplayValue = false)]
public int CityId { get; set; } public int CityId { get; set; }
[Display(Name = "City Name")]
[InputSize("medium")]
[Required]
public string Name { get; set; } public string Name { get; set; }
[Required]
public string County { get; set; } public string County { get; set; }
} }
} }
+33 -19
View File
@@ -238,33 +238,47 @@ namespace MileageTraker.Web.Utility
} }
public static Dictionary<string, List<string>> YearMonthList(IEnumerable<DateTime> dates) public static Dictionary<string, List<string>> YearMonthList(IEnumerable<DateTime> dates)
{
var tuples =
from d in dates
orderby d descending
select Tuple.Create(d.Year.ToString(), d.Month.ToString());
return GroupTuple(tuples);
//return
// (from d in dates
// orderby d.Year descending
// group d by d.Year
// into yg
// select
// new
// {
// Year = yg.Key.ToString(),
// MonthList =
// (from o in yg
// orderby o.Month descending
// group o by o.Month
// into mg
// select mg.Key.ToString()).ToList()
// }).ToDictionary(arg => arg.Year, arg => arg.MonthList);
}
public static Dictionary<string, List<string>> GroupTuple(IEnumerable<Tuple<string,string>> tuples)
{ {
return return
(from d in dates (from t in tuples
orderby d.Year descending group t by t.Item1
group d by d.Year
into yg into yg
select select
new new
{ {
Year = yg.Key.ToString(), Item1 = yg.Key,
MonthList = Item2List =
(from o in yg (from o in yg
orderby o.Month descending group o by o.Item2
group o by o.Month
into mg into mg
select mg.Key.ToString()).ToList() select mg.Key).ToList()
}).ToDictionary(arg => arg.Year, arg => arg.MonthList); }).ToDictionary(arg => arg.Item1, arg => arg.Item2List);
}
public static RouteValueDictionary ToRouteValueDictionary(this NameValueCollection collection)
{
var routeValueDictionary = new RouteValueDictionary();
foreach (var key in collection.AllKeys)
{
routeValueDictionary.Add(key, collection[key]);
}
return routeValueDictionary;
} }
//public static IEnumerable<SelectListItem> ToSelectList<T>( //public static IEnumerable<SelectListItem> ToSelectList<T>(
+30
View File
@@ -0,0 +1,30 @@
@model MileageTraker.Web.Models.City
@{
ViewBag.Title = "Create City";
}
<h2 class="center-content">@ViewBag.Title</h2>
@using (Html.BeginForm("Create", "City", FormMethod.Post, new { @class = "form-horizontal well center-content" }))
{
@Html.Partial("_ValidationSummary")
<fieldset>
<legend></legend>
@Html.DisplayFor(m => m.County)
@* <div class="control-group">
<div class="control-label">
@Html.DisplayNameFor(m => m.County)
</div>
<div class="controls">
@Html.DisplayTextFor(m => m.County)
</div>
</div>*@
@Html.HiddenFor(m => m.County)
@Html.EditorFor(m => m.Name)
<div class="form-actions">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</fieldset>
}
+25
View File
@@ -0,0 +1,25 @@
@model Dictionary<string,List<string>>
@{
ViewBag.Title = "Cities by County";
}
@Html.Partial("_StatusMessage")
<h2>@ViewBag.Title</h2>
<div>
@foreach (var countyCities in Model)
{
<div>
<h3>@countyCities.Key</h3>
@foreach (var city in countyCities.Value)
{
<span class="label">
@city
</span>
}
@Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn" })
</div>
}
</div>
+1
View File
@@ -36,6 +36,7 @@
@Html.ActionLink("Add New Log", "Create", null, new { @class = "btn" }) @Html.ActionLink("Add New Log", "Create", null, new { @class = "btn" })
@Html.ActionLink("Export", "Export", new { Model.Year, Model.Month, Model.LogType, Model.MonthRange, Model.VehicleId, Model.EmployeeName }, new { @class = "btn" }) @Html.ActionLink("Export", "Export", new { Model.Year, Model.Month, Model.LogType, Model.MonthRange, Model.VehicleId, Model.EmployeeName }, new { @class = "btn" })
@Html.ActionLink("Purposes", "Index", "Purpose", null, new { @class = "btn" }) @Html.ActionLink("Purposes", "Index", "Purpose", null, new { @class = "btn" })
@Html.ActionLink("Cities", "Index", "City", null, new { @class = "btn" })
@Html.ActionLink("Import", "ImportUpload", null, new { @class = "btn"}) @Html.ActionLink("Import", "ImportUpload", null, new { @class = "btn"})
<div class="btn-group"> <div class="btn-group">
+2
View File
@@ -257,6 +257,8 @@
<Content Include="Scripts\jquery.validate.min.js" /> <Content Include="Scripts\jquery.validate.min.js" />
<Content Include="Views\Log\ImportFix.cshtml" /> <Content Include="Views\Log\ImportFix.cshtml" />
<Content Include="Views\Shared\EditorTemplates\HttpPostedFileBase.cshtml" /> <Content Include="Views\Shared\EditorTemplates\HttpPostedFileBase.cshtml" />
<Content Include="Views\City\Index.cshtml" />
<Content Include="Views\City\Create.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Content\Account.Login.css" /> <Content Include="Content\Account.Login.css" />