Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created April 13, 2012 19:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PaulStovell/2379583 to your computer and use it in GitHub Desktop.
Save PaulStovell/2379583 to your computer and use it in GitHub Desktop.
@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)
<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<SelectListItem> Countries { get; set; }
}
class EditUserModelBuilder : IModelBuilder<EditUserModel, User>
{
readonly ISession session;
public EditUserModelBuilder(ISession session)
{
this.session = session;
}
public EditUserModel CreateFrom(User user)
{
var model = new EditUserModel();
model.Id = user.FirstName;
model.FirstName = user.FirstName;
model.LastName = user.LastName;
model.Country = user.Country.Id;
model.Countries = GetCountries();
return model;
}
public EditUserModel Rebuild(EditUserModel model)
{
model.Countries = GetCountries();
return model;
}
public User ApplyChanges(EditUserModel model)
{
var user = string.IsNullOrEmpty(model.Id) ? new User() : session.Find<User>(model.Id);
session.Store(user);
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.Country = session.Find<Country>(model.CountryId);
return user;
}
ICollection<SelectListItem> GetCountries()
{
var countries = session.FindAll<Country>();
return countries.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }).ToList();
}
}
interface IModelBuilder<TViewModel, TEntity>
{
TViewModel CreateFrom(TEntity entity);
TViewModel Rebuild(TViewModel model);
TEntity ApplyChanges(TViewModel viewModel);
}
class UserController : Controller
{
IModelBuilder<EditUserModel, User> builder = new EditUserModelBuilder();
public ActionResult Edit(string id)
{
var user = session.Find<User>(id) ?? new User();
return View(model, builder.CreateFrom(user));
}
[HttpPost]
public ActionResult Edit(EditUserModel model)
{
if (!ModelState.IsValid)
{
return View(builder.Rebuild(model);
}
builder.ApplyChanges(model);
session.SaveChanges();
return RedirectToAction("Index");
}
}
@nblumhardt
Copy link

Looks great :)

@dougrchamberlain
Copy link

There is another mistake in your IModelBuilder.cs line 4. It should be TEditUserModel

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