public
Last active

  • Download Gist
Edit.cshtml
C#
1 2 3 4 5 6 7 8 9 10 11 12 13
@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>
}
EditUserModel.cs
C#
1 2 3 4 5 6 7 8
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; }
}
EditUserModelBuilder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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();
}
}
IModelBuilder.cs
C#
1 2 3 4 5 6
interface IModelBuilder<TViewModel, TEntity>
{
TViewModel CreateFrom(TEntity entity);
TViewModel Rebuild(TViewModel model);
TEntity ApplyChanges(TViewModel viewModel);
}
UserController.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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");
}
}

This approach is based on this StackOverflow answer: http://stackoverflow.com/a/2775656/57132

I like it because the controller is much simpler, and the save code is more easily reused. In the real world I'd inject the model builder so testing would be easier too. I like that the messiness of re-filling the countries list for example is hidden inside the builder.

Agree that this was a tidier approach. Just wanted to mention, there's a copy/paste bug in Edit.cshtml line 8.

Thanks Peter, I've fixed the bug

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.