的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