Created
April 13, 2012 19:19
-
-
Save PaulStovell/2379363 to your computer and use it in GitHub Desktop.
How can I clean up this simple ASP.NET MVC pattern?
This file contains 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 characters
@model EditUserModel | |
@using (Html.BeginForm()) | |
{ | |
<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" /> | |
</div> | |
} |
This file contains 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 characters
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 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 characters
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); | |
} | |
[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)).ToList(); | |
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"); | |
} |
Here is another:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an alternative:
https://gist.github.com/2379583