1

i want to create a viewmodel to access in my view to two differents models i have created.

For this i create two diferrents models and one model which i include both.

My problem is that in my view i can´t access the data.

hope that anybody can help with this.

What i need to represented in my view are: Table1 : name title

Table2: picpath of each image

here is my code:

model 1:

 public class Table1
    {
        public int ID { get; set; }
        public string name{ get; set; }
        public string title { get; set; }
        public string edition{ get; set; }
        public string number{ get; set; }

    }

   public class DefaultConnection : DbContext
    {
        public DbSet<Table1> Res{ get; set; }
    }

model 2:

 public class Images
    {
        public SelectList ImageList { get; set; }   
        public int ID { get; set; }
        public string title{ get; set; }
        public string picpath { get; set; }

        public Img)
        {
            ImageList = GetImages();
        }

        public SelectList GetImages()
        {
            var list = new List<SelectListItem>();
            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (var con = new SqlConnection(connection))
            {
                con.Open();
                using (var command = new SqlCommand("SELECT * FROM Myimages", con))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string title = reader[1] as string;
                        string imagePath = reader[2] as string;
                        list.Add(new SelectListItem() { Text = title, Value = imagePath });
                    }
                }
                con.Close();
            }
            return new SelectList(list, "Value", "Text");
        }

    }

MY VIEW Model:

 public class ViewModel
    {
        public Table1 table1{ get; set; }
        public Images xpto { get; set; }

        public ViewModel(Table1 table1)
        {
            Table1 = table1;
            xpto = new Images();
        }
    }



**Controller:**

 public ActionResult HotSpotMaker(int id = 0)
        {
            Table1 rev = db.Res.Find(id);          
            if (rev == null)
            {
                return HttpNotFound();
            }
//Here is something missing, have delete my version here because don´t make any sense
            return View(rev);
        }

View:

  @model myproject.Models.ViewModel

Note: i search a lot, and find that a lot of people use this: @model myproject.Web.Models.ViewModel , but i can´t select this web. . i don´t know if this is relevant or not, i thought maybe is important to say it.

4

2 回答 2

3

Table1作为视图模型传入,但这不是您的视图模型。

尝试:

return View(new Models.ViewModel(rev));
于 2013-08-02T13:43:54.247 回答
1

好吧,这很简单,您View期待 a ViewModel(顺便说一句,这是一个坏名字,但我怀疑这只是一个测试),但是您给它 a Table1,您的视图根本不知道如何处理它。

您需要一种方法来ViewModel从对象创建Table1对象

在您的情况下,只需像这样调用您的构造函数

return View(new ViewModel(rev));
于 2013-08-02T13:48:50.397 回答