Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created April 13, 2012 19:19

Revisions

  1. PaulStovell revised this gist Apr 13, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Edit.cshtml
    Original 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.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" />
  2. PaulStovell revised this gist Apr 13, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions UserController.cs
    Original 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)));
    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));
    model.Countries = countries.Select(c => CountryModel.CreateFrom(c)).ToList();
    return View(model);
    }

  3. PaulStovell revised this gist Apr 13, 2012. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions Edit.cshtml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    @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>

    @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>
    }
  4. PaulStovell created this gist Apr 13, 2012.
    7 changes: 7 additions & 0 deletions Edit.cshtml
    Original 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>
    33 changes: 33 additions & 0 deletions Models.cs
    Original 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;
    }
    }
    31 changes: 31 additions & 0 deletions UserController.cs
    Original 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");
    }