2012-05-02 13 views
1

我需要建立某種矩陣的從A點去所需的天數到B點。需要進行數據錄入的一種矩陣

這應該是這樣的:

enter image description here

每個起點/終點點被存儲在表(名爲停止):

public class Stop 
{ 
    [Key] 
    public int StopID { get; set; } 
    public string StopCode { get; set; } 
} 

矩陣的數據應當被存儲在另一個表中:

public class Matrix 
{ 
    [Key] 
    public int MatrixID { get; set; } 
    public int OriginStopID { get; set; } 
    public int DestinationStopID { get; set; } 
    public int NumberOfDays { get; set; } 
} 

我需要一個視圖來允許用戶輸入這個矩陣中的數據。 我該如何做到這一點?我不知道。

謝謝。

+0

你有沒有這方面的任何解決方案?我也有這樣的問題。 –

回答

0

U可以使用基於兩個viewmodels的複雜模型來創建表單或使用基於數組的簡單東西。

這是一個非常簡單的例子,可以做什麼(如果需要,我可以用兩個ViewModel做出更復雜的例子)。

視圖模型:

public class TestViewModel 
{ 
    public int[][] Matrix { get; set; } 

    public TestViewModel() 
    {    
    } 

    public TestViewModel(string i) 
    { 
     Matrix = new int[4][] { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 4 } }; 
    } 
} 

控制器:

public ActionResult Test() 
    { 
     return View(new TestViewModel("sa")); 
    } 

    [HttpPost] 
    public ActionResult Test(TestViewModel model) 
    { 
     //some business logic goes here 
     //ViewModel to Model convertion goes here 
     return View(model); 
    } 

查看:

@model TestViewModel 
@using (Html.BeginForm()) 
{ 
    <div class="form_block"> 
     @for (int i = 0; i<Model.Matrix.Length;i++) 
     { 
      for (int j = 0; j<Model.Matrix[i].Length;j++) 
      { 
        @Html.TextBoxFor(x=>x.Matrix[i][j]) 
      } 
     } 
    </div> 
    <div class="submit_block"> 
     <input type="submit" value="Сохранить изменения" /> 
    </div> 
} 
+0

謝謝。我會盡快嘗試你的解決方案,我會保持聯繫。 – Bronzato