Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created April 13, 2012 19:19
Show Gist options
  • Save PaulStovell/2379363 to your computer and use it in GitHub Desktop.
Save PaulStovell/2379363 to your computer and use it in GitHub Desktop.
How can I clean up this simple ASP.NET MVC pattern?
@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>
}
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;
}
}
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");
}
@PaulStovell
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment