Created
April 13, 2012 19:19
Revisions
-
PaulStovell revised this gist
Apr 13, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ <div> @Html.HiddenFor(m => m.Id) @Html.TextBoxFor(m => m.FirstName) @Html.TextBoxFor(m => m.LastName) @Html.DropDownListFor(m => m.CountryId, Model.Countries.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }) <input type="submit" /> -
PaulStovell revised this gist
Apr 13, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ public ActionResult Edit(string id) var user = string.IsNullOrEmpty(id) ? session.Find<User>(id) : new User(); var countries = session.FindAll<Country>(); var model = EditUserModel.CreateFrom(user, countries.Select(c => CountryModel.CreateFrom(c)).ToList()); return View(model); } @@ -14,7 +14,7 @@ public ActionResult Edit(EditUserModel model) { // Since the posted data won't contain the country list, we have to re-fill the model. This code feels hacky var countries = session.FindAll<Country>(); model.Countries = countries.Select(c => CountryModel.CreateFrom(c)).ToList(); return View(model); } -
PaulStovell revised this gist
Apr 13, 2012 . 1 changed file with 12 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,13 @@ @model EditUserModel @using (Html.BeginForm()) { <div> @Html.HiddenFor(m => m.Id) @Html.TextBoxFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName) @Html.DropDownListFor(m => m.CountryId, Model.Countries.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }) <input type="submit" /> </div> } -
PaulStovell created this gist
Apr 13, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ @model EditUserModel <div> @Html.HiddenFor(m => m.Id) @Html.TextBoxFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName) @Html.DropDownListFor(m => m.CountryId, Model.Countries.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }) </div> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ class EditUserModel { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string CountryId { get; set; } public ICollection<CountryModel> Countries { get; set; } public static EditUserModel CreateFrom(User user, ICollection<CountryModel> countries) { var model = new EditUserModel(); model.Id = user.FirstName; model.FirstName = user.FirstName; model.LastName = user.LastName; model.Country = user.Country.Id; model.Countries = countries; return model; } } class CountryModel { public string Id { get; set; } public string Name { get; set; } public static CountryModel CreateFrom(Country country) { var model = new CountryModel(); model.Id = country.Id; model.Name = country.Name; return model; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ public ActionResult Edit(string id) { var user = string.IsNullOrEmpty(id) ? session.Find<User>(id) : new User(); var countries = session.FindAll<Country>(); var model = EditUserModel.CreateFrom(user, countries.Select(c => CountryModel.CreateFrom(c))); return View(model); } [HttpPost] public ActionResult Edit(EditUserModel model) { if (!ModelState.IsValid) { // Since the posted data won't contain the country list, we have to re-fill the model. This code feels hacky var countries = session.FindAll<Country>(); model.Countries = countries.Select(c => CountryModel.CreateFrom(c)); return View(model); } var user = string.IsNullOrEmpty(model.Id) ? session.Find<User>(model.Id) : new User(); session.Store(user); user.FirstName = model.FirstName; user.LastName = model.LastName; // Messy handling for referenced entity user.Country = session.Find<Country>(model.CountryId); session.SaveChanges(); return RedirectToAction("Index"); }