5

我有一个视图模型,它需要来自两个模型人员和地址的数据:

楷模:

public class Person
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
   public int Gender { get; set; }
}

public class Address
{
   public int Id { get; set; }
   public string Street { get; set; }
   public int Zip { get; set; }
   public int PersonId {get; set; }
}

Viewmodel 就是这样

public class PersonAddViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
}

我尝试了几种方法将数据放入视图模型并将其传递给视图。将有多个记录返回显示。

我的最新方法是这样填充视图模型:

private AppContexts db = new AppContexts();
public ActionResult ListPeople()
{
    var model = new PersonAddViewModel();
    var people = db.Persons;
    foreach(Person p in people)
    {
        Address address = db.Addresses.SingleOrDefault(a => a.PersonId == p.Id)
        model.Id = p.Id;
        model.Name = p.Name;
        model.Street = address.Street;
    }
    return View(model.ToList());
}

我在 Address address = db... 行“EntityCommandExecutionException 未被用户代码处理。

如何用多条记录填充视图模型并传递给视图?

最终解决方案:

private AppContexts db = new AppContexts();
private AppContexts dbt = new AppContexts();
public ActionResult ListPeople()
{
    List<PersonAddViewModel> list = new List<PersonAddViewModel>();
    var people = db.Persons;
    foreach(Person p in people)
    {
        PersonAddViewModel model = new PersonAddViewModel();
        Address address = dbt.Addresses.SingleOrDefault(a => a.PersonId == p.Id)
        model.Id = p.Id;
        model.Name = p.Name;
        model.Street = address.Street;
    }
    return View(list);
}
4

2 回答 2

5

首先,EntityCommandExecutionException 错误表明您的实体上下文或实体本身的定义中存在错误。这会引发异常,因为它发现数据库与您告诉它的方式不同。你需要弄清楚这个问题。

其次,关于执行此操作的正确方法,如果您的上下文配置正确,您显示的代码应该可以工作。但是,更好的方法是使用 Navigational 属性,只要您想要获取所有相关记录并且不指定其他 Where 子句参数。导航属性可能如下所示:

public class Person
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
   public int Gender { get; set; }

   public virtual Address Address { get; set; }
   // or possibly, if you want more than one address per person
   public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
   public int Id { get; set; }
   public string Street { get; set; }
   public int Zip { get; set; }
   public int PersonId { get; set; }

   public virtual Person Person { get; set; }
}

然后你会简单地说:

public ActionResult ListPeople()
{
    var model = (from p in db.Persons // .Includes("Addresses") here?
                select new PersonAddViewModel() {
                    Id = p.Id,
                    Name = p.Name,
                    Street = p.Address.Street,
                    // or if collection
                    Street2 = p.Addresses.Select(a => a.Street).FirstOrDefault()
                });

    return View(model.ToList());
}
于 2013-04-18T05:50:08.253 回答
2

为了显示对象列表,您可以使用具有通用列表的通用视图模型:

public class GenericViewModel<T>
{
    public List<T> Results { get; set; }

    public GenericViewModel()
    {
        this.Results = new List<T>();
    }
}

有一个返回的控制器操作,比如说你数据库中的所有人:

[HttpGet]
public ActionResult GetAllPeople(GenericViewModel<People> viewModel)
{
    var query = (from x in db.People select x); // Select all people
    viewModel.Results = query.ToList();

    return View("_MyView", viewModel);
}

然后使您的视图具有强类型,并采用您的通用视图模型:

@model NameSpace.ViewModels.GenericViewModel<NameSpace.Models.People>
于 2013-04-17T23:05:17.167 回答