2013-08-02 140 views
1

我想創建一個視圖模型以在我的視圖中訪問我創建的兩個不同模型。如何使用2個不同模型創建視圖模型

爲此,我創建了兩個diferrents模型和一個模型,其中包括兩者。

我的問題是,在我看來我無法訪問數據。

希望任何人都可以提供幫助。

我需要什麼,我認爲到代表有: 表1: 名 標題

表2: picpath每個圖像

這裏是我的代碼:

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

模式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"); 
     } 

    } 

我的看法型號:

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

查看:

@model myproject.Models.ViewModel 

注:我搜索了很多,發現很多人們使用這個:@model myproject.Web.Models.ViewModel,但我不能選擇這個網站。 。我不知道這是否相關,我想也許很重要。

+0

你可以參考我的問題2不同的方式來實現這一目標。 http://stackoverflow.com/questions/17502294/what-is-the-proper-way-to-submit-data-from-parent-form-with-partial-view-mvc-4 –

回答

3

你正在通過Table1作爲視圖模型,但這不是你的視圖模型。

嘗試:

return View(new Models.ViewModel(rev)); 
+0

hmm ok ...但現在我有另一個錯誤:ViewModel不包含「名稱」的定義,並且不接受類型ViewModel的第一個參數,因爲沒有找到 – user2232273

+2

@ user2232273,那麼某個地方(也許在視圖中)您正在調用'name'模型,但它沒有名稱屬性。它有'table1'和'xpto',所以我懷疑你需要調用'@ Model.table1.name'。 –

+0

thx它的作品...在我的這個@ Html.DisplayFor(模型=>模型。名稱)並將其更改爲@ table1.xpto.name ..thx很多 – user2232273

1

那麼它的相當簡單的,你View期待一個ViewModel(這是一個不好的名字的方式,但我懷疑這只是一個測試),但你給它一個Table1,你的觀點根本不知道如何處理它。

您需要一種方法來從Table1對象

在你的情況下創建一個ViewModel對象只需調用構造函數,像這樣

return View(new ViewModel(rev)); 
+0

thx西蒙的職位..名稱viewmodel只是一個測試..我嘗試瞭解這是如何工作在mvc 4 .....我嘗試它,但現在給另一個錯誤..你能看到我的評論@ moo果汁...我現在解釋什麼是錯誤 – user2232273

+1

這是因爲你的觀點是畸形的,你需要做的不僅僅是改變@ @using myproject.Models.Table1'到'@using myproject.Models.ViewModel',你的'ViewModel'對象沒有name屬性,但它包含一個'Table1'對象。 通過@ Html.DisplayNameFor(model => model.table1.name)嘗試更改視圖中的東西,比如'@ Html.DisplayNameFor(model => model.name)'' 請記住,視圖模型沒什麼特別的,它們是隻是簡單的對象,「視圖模型」只是一個命名約定來指定他們的角色 –

+0

是的,我已經在我的視圖中進行了改變...我之前@ Html.DisplayFor(model => model.n_empresa)這不工作..我改變它到這個@ table.xpto.name ... – user2232273

相關問題