Add required city field

This commit is contained in:
2014-05-21 10:17:05 -04:00
parent b2f8baaca6
commit 365aa47328
10 changed files with 70 additions and 41 deletions
+30 -39
View File
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
@@ -8,13 +9,13 @@ namespace MileageTraker.Web.Controllers
{
[Authorize(Roles = "Administrator, Developer")]
public class CityController : ControllerBase
{
{
[Authorize(Roles = "Administrator, Developer, Driver")]
public JsonResult Autocomplete(string term)
{
{
var cities = DataService.GetCitiesAutocomplete(term);
return Json(cities, JsonRequestBehavior.AllowGet);
}
return Json(cities, JsonRequestBehavior.AllowGet);
}
public ActionResult Index()
{
@@ -26,46 +27,36 @@ namespace MileageTraker.Web.Controllers
public ActionResult Create(string county)
{
var city = new City { County = 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);
[ActionLog]
[HttpPost]
public ActionResult Create([Bind(Exclude = "CityId")] City city)
{
if (ModelState.IsValid)
{
DataService.AddCity(city);
// TempData["StatusMessage"] = "City " + city.Name + " created";
// return RedirectToAction("Index");
// }
TempData["StatusMessage"] = "City " + city.Name + " created";
return RedirectToAction("Index");
}
// return View(city);
//}
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);
//}
public JsonResult CityNameAvailable(string name)
{
var city = DataService.FindCityByName(name);
return Json(city == null, JsonRequestBehavior.AllowGet);
}
//[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);
//}
}
[Authorize(Roles = "Administrator, Developer, Driver")]
public JsonResult CityNameExists(string cityname)
{
var city = DataService.FindCityByName(cityname);
return Json(city != null, JsonRequestBehavior.AllowGet);
}
}
}
+10
View File
@@ -270,6 +270,16 @@ namespace MileageTraker.Web.Controllers
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// verify city exists
if (DataService.FindCityByName(log.CityName) == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "City must exist in system",
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// SAVE IT!
DataService.AddLog(log);
+10
View File
@@ -350,6 +350,16 @@ namespace MileageTraker.Web.Controllers
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// verify city exists
if (DataService.FindCityByName(log.CityName) == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "City must exist in system",
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// SAVE IT!
DataService.AddLog(log);
+12
View File
@@ -492,6 +492,17 @@ namespace MileageTraker.Web.DAL
return _db.Cities;
}
public City FindCityByName(string name)
{
return _db.Cities.FirstOrDefault(u => name.Equals(u.Name, StringComparison.InvariantCultureIgnoreCase));
}
public void AddCity(City city)
{
_db.Cities.Add(city);
_db.SaveChanges();
}
#endregion
#region Membership
@@ -595,5 +606,6 @@ namespace MileageTraker.Web.DAL
}
#endregion
}
}
+1
View File
@@ -12,6 +12,7 @@ namespace MileageTraker.Web.Models
[Display(Name = "City Name")]
[InputSize("medium")]
[Required]
[Remote("CityNameAvailable", "City", ErrorMessage = "City name already in use")]
public string Name { get; set; }
[Required]
public string County { get; set; }
@@ -37,6 +37,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog
[Display(Name = "Destination City")]
[InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; }
[Display(Name = "Purpose")]
@@ -39,6 +39,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog
[Display(Name = "Destination City")]
[InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; }
[Display(Name = "Purpose")]
+1
View File
@@ -47,6 +47,7 @@ namespace MileageTraker.Web.ViewModels.Log
[Display(Name = "City Name")]
[InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; }
[Display(Name = "Purpose")]
+3 -1
View File
@@ -13,13 +13,15 @@
{
<div>
<h3>@countyCities.Key</h3>
<p>
@foreach (var city in countyCities.Value)
{
<span class="label">
@city
</span>
}
@Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn" })
@Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn btn-small" })
</p>
</div>
}
</div>
+1 -1
View File
@@ -46,7 +46,7 @@
@RenderBody()
</div>
<footer>
Mileage Traker &copy; 2013 James Kolpack
Mileage Traker &copy; 2014 Kolpack Software Consulting LLC
</footer>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")" type="text/javascript"></script>