的ViewModels是代表了一個視圖,你需要顯示的模型信息數據的方法,請看看這個問題,瞭解它們是什麼:What is ViewModel in MVC?
你的情況,你可以創建
namespace MyProject
{
public class MyViewModel
{
public int ID { get; set; }
public System_Tracking Tracking { get; set; }
public Report_Login_Credentials Credentials { get; set; }
public Report_Type Type { get; set; }
public List<Report_Peramiter_Fields> Fields { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
}
它可能只是看起來像一個簡單的模型,而且因爲它是,但它只是一個視圖中使用它:一個簡單的視圖模型來表示你需要顯示在Report_Main
數據,這可以很容易做的實現不是持續存在。這是模型和視圖模型之間的區別。
從這裏就只需要一個頁面,說MyPage.cshtml
在那裏你可以使用這個模型,如:
@model MyProject.MyViewModel
@{
ViewBag.Title = "MyPage";
}
@* your content here *@
@Model.ID <br>
@Model.Report_Type.Description
// Etc.
要通過這些信息,您將需要做的是在你的控制器,例如:
namespace MyProject
{
public class MyController : Controller
{
public ActionResult MyPage(int? Id)
{
var data = context.Report_Main.Find(Id); // Or whatever
var vm = new MyViewModel(){
Tracking = data.System_Tracking,
// ... populate the viewmodel here from the data received
};
return View(vm);
}
}
}
簡而言之,ViewModels允許您綁定一個模型中的所有數據,並將其傳遞到可以靜態表示它的視圖上,並且還可以在不想直接表示的情況下從用戶獲取數據時使用它。訪問模型。
編輯:
foreach(var item in Model.Fields)
{
<p>item.Peramiter</p>
}
什麼是你的基礎數據層包括:您可以通過使用C#通過在刀片型號列表循環?你在使用實體框架嗎? NHibernate的?請給一些更多的細節。 – px06
道歉,我目前正在使用實體框架和SQL服務器 – Houlahan