2

我想养成使用的习惯ViewModels。过去我只在我的中使用它们,Create Actions我从未想过如何在Edit Actions. 我Domain Entities改用了。

假设我有以下内容:

使用Entity Framework Code First

域项目中的POCO 类

public class Person
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PersonId { get; set; }

    public string Name { get; set; }        

    public string Website { get; set; }

    public DateTime? Created { get; set; }

    public DateTime? Updated { get; set; }
}

在我的数据项目中

摘要文件夹:

public interface IPersonRepository
{
    IQueryable<Person> People{ get; }  
    void SavePerson(Person person);     
}

具体文件夹:

EfDb 类

public class EfDb : DbContext
{
    public EfDb() : base("DefaultConnection") {}

    public DbSet<Person> People{ get; set; }
}

EfPersonRepository 类

#region Implementation of Person in IPersonRepository

public IQueryable<Person> People
{
    get { return _context.People; }
}

public void SavePerson(Persona person)
{
    if (person.PersonId == 0)
    {
        _context.People.Add(person);
    }
    else if (person.PersonId> 0)
    {
        var currentPerson = _context.People
            .Single(a => a.PersonId== person.PersonId);

        _context.Entry(currentPerson).CurrentValues.SetValues(person);
    }

    _context.SaveChanges();
}

#endregion

WebUI 项目 ViewModels 文件夹中的PersonCreateViewModel

public class PersonCreateViewModel
{
    [Required]
    [Display(Name = "Name:")]
    public string Name { get; set; }        

    [Display(Name = "Website:")]
    public string Website { get; set; }
}

人员控制器和创建动作:

public class PersonController : Controller
{
    private readonly IPersonRepository _dataSource;

    public PersonController(IPersonRepository dataSource)
    {
        _dataSource = dataSource;
    }

    // GET: /Association/
    public ActionResult Index()
    {
        return View(_dataSource.Associations);
    }

    // GET: /Person/Details/5
    public ActionResult Details(int id)
    {
        return View();
    }

    // GET: /Person/Create
    [HttpGet]
    public ActionResult Create()
    {
       return View();
    }

    // POST: /Person/Create
    [HttpPost]
    public ActionResult Create(PersonCreateViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                var Person = new Person
                      {
                          Name = Model.Name,
                          Website = model.Website,
                          Created = DateTime.UtcNow,
                          Updated = DateTime.UtcNow
                      };

                _dataSource.SavePerson(person);
                return RedirectToAction("Index", "Home");
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes. ");
            }
        }

        return View(model);
    }
 }

现在,除非我弄错了,否则我希望 myPersonEditViewlModel看起来完全像我的PersonCreateViewlModel. 但我不知道如何在我的Edit行动中使用它,前提是我也必须SavePerson(Person person)像我在Create行动中那样打电话。

注意:请不要对AutoMapper或提出建议ValueInjecter

这是怎么做到的?

4

1 回答 1

2

它就像创建一样,只是你需要记录 ID。

[HttpGet]
public ActionResult Edit(int id)
{
    var personVm = _dataSource.People.Single(p => p.PersonId == id)
        .Select(e => new PersonEditViewModel {
                    e.PersonId = p.PersonId,
                    e.Name = p.Name,
                    e.Website = p.Website
                    ...
                });
    return View(personVm);
}

[HttpPost]
public ActionResult Edit(PersonEditViewModel model)
{
    if (ModelState.IsValid)
    {
        var person = _dataSource.People.Single(p => p.PersonId == model.PersonId);
        person.Name = model.Name;
        person.Website = model.Website;
        ...
        _dataSource.EditPerson(person);
        return RedirectToAction("Index", "Home");
    }
    return View(model);
}

编辑: 因此您无需对编辑进行其他查询

public void EditPerson(Person person)
{
    _context.Entry(person).State = EntityState.Modified;
    _context.SaveChanges();
}
于 2013-04-15T18:33:00.590 回答