MVC4 C#从数据库中的视图模型填充数据 [英] MVC4 C# Populating data in a viewmodel from database

查看:293
本文介绍了MVC4 C#从数据库中的视图模型填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型的人,地址需要数据的视图模型:

模型:

 公共类Person
{
   公众诠释标识{搞定;组; }
   公共字符串名称{;组; }
   公众诠释年龄{搞定;组; }
   公众诠释性别{搞定;组; }
}公共类地址
{
   公众诠释标识{搞定;组; }
   公共字符串街{搞定;组; }
   公众诠释邮编{搞定;组; }
   公众诠释PERSONID {搞定;组; }
}

视图模型是这样

 公共类PersonAddViewModel
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公共字符串街{搞定;组; }
}

我曾尝试多种方法来获取数据到视图模型,并把它传递给视图。将有多个返回的记录显示。

我的最新方法填充视图模型,例如:

 私人AppContexts DB =新AppContexts();
公众的ActionResult ListPeople()
{
    VAR模型=新PersonAddViewModel();
    变种人= db.Persons;
    的foreach(在人者P)
    {
        地址地址= db.Addresses.SingleOrDefault(A => a.PersonId == p.Id)
        model.Id = p.Id;
        model.Name = p.Name;
        model.Street = address.Street;
    }
    返回查看(model.ToList());
}

我得到的地址地址= DB错误...... EntityCommandExecutionException的行是由用户code未处理。

你怎么能填充多个记录视图模型,并传递给一个看法?

最终解决方案:

 私人AppContexts DB =新AppContexts();
私人AppContexts DBT =新AppContexts();
公众的ActionResult ListPeople()
{
    清单< PersonAddViewModel>名单=新名单,LT; PersonAddViewModel>();
    变种人= db.Persons;
    的foreach(在人者P)
    {
        PersonAddViewModel模式=新PersonAddViewModel();
        地址地址= dbt.Addresses.SingleOrDefault(A => a.PersonId == p.Id)
        model.Id = p.Id;
        model.Name = p.Name;
        model.Street = address.Street;
    }
    返回查看(名单);
}


解决方案

首先,EntityCommandExecutionException错误表明在实体环境或实体本身的定义错误。因为它的发现数据库从你告诉它,它应该是不同的方式,这是抛出异常。你需要弄清楚这个问题。

第二,关于正确的方式做到这一点,你已经显示的code应该工作,如果你的环境配置正确。但是,一个更好的办法是使用导航性能,只要你想获得所有相关的记录,而不是其他指定Where子句的参数。一个导航属性​​可能是这样的:

 公共类Person
{
   公众诠释标识{搞定;组; }
   公共字符串名称{;组; }
   公众诠释年龄{搞定;组; }
   公众诠释性别{搞定;组; }   公共虚拟地址地址{搞定;组; }
   //或者可能是,如果你想每人多个地址
   公共虚拟的ICollection<地址>地址{搞定;组; }
}公共类地址
{
   公众诠释标识{搞定;组; }
   公共字符串街{搞定;组; }
   公众诠释邮编{搞定;组; }
   公众诠释PERSONID {搞定;组; }   公共虚拟人人{搞定;组; }
}

那你就简单的说:

 公众的ActionResult ListPeople()
{
    VAR模型=(上接第这里db.Persons // .Includes(地址)?
                选择新PersonAddViewModel(){
                    ID = p.Id,
                    名称= p.Name,
                    街= p.Address.Street,
                    //或者,如果集合
                    STREET2 = p.Addresses.Select(一个= GT; a.Street).FirstOrDefault()
                });    返回查看(model.ToList());
}

I have a viewmodel which needs data from two models person and address:

Models:

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; }
}

The Viewmodel is as such

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

I have tried several ways to get data into the viewmodel and pass it to the view. There will be multiple records returned to display.

My latest method is populating the view model as such:

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());
}

I get an error on the Address address = db... line of "EntityCommandExecutionException was unhandled by user code.

How can you populate a view model with multiple records and pass to a view?

Final Solution:

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);
}

解决方案

First, EntityCommandExecutionException errors indicates an error in the definition of your entity context, or the entities themselves. This is throwing an exception because it's found the database to be different from the way you told it that it should be. You need to figure out that problem.

Second, regarding the proper way to do this, the code you've shown should work if your context were correctly configured. But, a better way would be to use Navigational properties, so long as you want to get all related records and not specify other Where clause parameters. A navigational property might look like this:

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; }
}

Then you would simply say:

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());
}

这篇关于MVC4 C#从数据库中的视图模型填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆