加入两款车型将数据放入一个视图 [英] Join two models to get data into a view
问题描述
完整MVC菜鸟警告。(2小时学习时间)
我已经看了很多MVC3在线的例子,但我还没有找到一个简单的例子做什么,我试图做的。
我想要做的就是两个连接两种型号,并得到一些数据的视图。最明显的
公共部分类模型1
{
公众诠释ID {搞定;组; }
公众诠释StudentID {搞定;组; }
公众诠释CoachID {搞定;组; }
公共字符串StudentName {获取;集;}}公共部分类模型2
{
公众诠释CoachID {搞定;组; }
公共字符串CoachName {搞定;组; }
}
基本上我认为我必须快快加入模型1和模式2 CoachID和打印
StudentName和CoachName在一个网格。
我如何做到这一点?如何创建视图模型此?
如何通过视图迭代和打印连接的数据?
我可以而不只是在数据库中创建一个视图,并直接连接一个模型和一个视图呢?
块引用>听起来简单,但香港专业教育学院度过了最后三个小时上网彻底难倒了
解决方案
- 系统StudentCoachViewModel
创建正是你在它需要的属性显示,仅此而已,无所不及。
填充此视图模型在你的控制器列表,并将其发送到您的视图。 code样品接踵而至。
枚举您认为列表
公共类StudentCoachViewModel
{
公共字符串CoachName {搞定;组; }
公共字符串StudentName {获取;设置; }
}在你的控制器东西大致如下(刚输入了这一点,还没有检查编译)
公众的ActionResult指数()
{ // code来填充你的MODEL1和MODEL2已经假设 变种的ViewModels =(从model1List米
加入R IN model2List上m.CoachId等于r.CoachId
选择新StudentCoachViewModel(){StudentName = m.StudentName,
CoachName = r.CoachName})了ToList()。 返回查看(的ViewModels);
}在你看来,沿(显然要格式化并使用适当的布局,表格等,可以由Visual Studio生成自动)线的东西。
@model IEnumerable的< StudentCoachViewModel>
//其他html内容点击这里@foreach(以VAR模型视图模型)
{
@ Html.DisplayFor(O => o.CoachName)@ Html.DisplayFor(O => o.StudentName)
}现在,如果你只是想这里单一,而不是一个列表中的更容易
公众的ActionResult指数(INT ID)
{
// code加载MODEL1和MODEL2已经假定到位。还假设您加载由ID字段的数据库这个数据传递到这个方法。
返回查看(新StudentCoachViewModel(){StudentName = model1.StudentName,CoachName = model2.CoachName});
}然后在视图变得简单
@model StudentCoachViewModel
//其他HTML这里,H1的div等无论是在您的视图HTML内容。
@ Html.EditorForModel()
或者,如果你想显示的每一个,而不是上面的一行电话:
@ Html.LabelFor(O => o.CoachName)
@ Html.EditorFor(O => o.CoachName)Complete MVC Noob warning.(2 hours learn time)
I've looked at a lot of MVC3 examples online but I havent found a simple example to do what I am trying to do.
What I want to do is two join two models and get some data into a view. The most obvious
public partial class Model1 { public int ID { get; set; } public int StudentID { get; set; } public int CoachID { get; set; } public String StudentName {get;set;} } public partial class Model2 { public int CoachID { get; set; } public String CoachName { get; set; } }
Basically in my view I have to just join Model 1 and model 2 on CoachID and print StudentName and CoachName in a grid.
How do I do this? How do I create the view model for this ?
How do I iterate through the view and print the joined data?
Can I instead just create a view in the database and directly attach a model and a view to it ?
Sounds simple, but Ive spent the last three hours online completely baffled
解决方案
Create a StudentCoachViewModel with exactly the properties you need in it to display, nothing more, nothing less.
Populate a list of this viewmodel in your controller and send it to your view. Code sample to follow shortly.
Enumerate that list in your view
public class StudentCoachViewModel { public string CoachName { get; set; } public string StudentName { get;set; } }
In your controller something along the following lines (just typing this out, haven't checked in compiler)
public ActionResult Index() { //code to populate your model1 and model2 already assumed var viewModels = (from m in model1List join r in model2List on m.CoachId equals r.CoachId select new StudentCoachViewModel(){ StudentName=m.StudentName, CoachName = r.CoachName }).ToList(); return View(viewModels); }
In your view, something along the lines of (clearly you want to format and use proper layout, table, etc which can be auto generated by visual studio)
@model IEnumerable<StudentCoachViewModel> //other html content here @foreach(var viewModel in Model) { @Html.DisplayFor(o=>o.CoachName) @Html.DisplayFor(o=>o.StudentName) }
Now if you are only wanting a single one here rather than a list its even easier
public ActionResult Index(int id) { //code to load model1 and model2 already assumed to be in place. also assuming you loaded this data from a database by the id field being passed into this method. return View(new StudentCoachViewModel(){ StudentName = model1.StudentName, CoachName = model2.CoachName}); }
And the view becomes then simply
@model StudentCoachViewModel //other html here, h1, divs, etc whatever is in your view as html content. @Html.EditorForModel() or if you like to display each one instead of the above one line call: @Html.LabelFor(o=>o.CoachName) @Html.EditorFor(o=>o.CoachName)
这篇关于加入两款车型将数据放入一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!