Mvc用两种模型显示视图中的数据。 [英] Mvc to show the data in view with two models.
问题描述
我有一个正常类的模型,只适用于强类型视图。
我被卡住了。我想在一个视图中显示两个模型数据。一个很简单,另一个来自数据库(checkBoxList)一个。
请帮忙。
我尝试了什么:
I have a model which is normal a class just for a strongly typed view.
I am stuck.I want to show two model data in one View.one is simple and the other is coming from database (checkBoxList ) one.
Pls help.
What I have tried:
public class ProcessSelectDate
{
[Required]
public string fromdate { get; set; }
[Required]
public string Todate { get; set; }
//Processes
}
和另一个通过实体框架的模型
and another model that is through Entity framework
public partial class Process
{
public decimal id { get; set; }
public string ProcessName { get; set; }
public bool Ischecked { get; set; }
}
我想在视图中显示来自数据库的processname的复选框列表。
所以我的观点将是
for textbox和checkboxList,另外还有两个型号
I want to show the checkboxlist of processname in The view which is coming from database.
so my view will be
for textbox and checkboxList, moreover two models
@Html.TextBoxFor(model => model.FromDate)
</div> <br />
<div><span class="RequiredField">* </span>To Date:</div>
<div>
@Html.TextBoxFor(model => model.ToDate)
</div> <br />
@for(int i = 0; i< Model.processName.Count; i ++)
{
@for (int i = 0; i < Model.processName.Count; i++)
{
@ Html.HiddenFor(x => x.processName [i] .Name)
@ Html.CheckBoxFor(x => x.processName [i ] .Checked)
@ Html.LabelFor(x => x.processName [i] .Checked,Model.processName [i] .Name)
@Html.HiddenFor(x => x.processName[i].Name)
@Html.CheckBoxFor(x => x.processName[i].Checked)
@Html.LabelFor(x => x.processName[i].Checked, Model.processName[i].Name)
}
和控制器
dbcontext db = new dbcontext();
db.Processes.ToList(); //
}
and in controller
dbcontext db = new dbcontext();
db.Processes.ToList(); //
推荐答案
从Richards发布解决方案评论
控制器
Posting as Solution From Richards Comment
Controller
public class ModelContainer
{
public Model1 Model1Property { get; set; }
public Model1 Model2Property { get; set; }
}
public class Model1
{
public int Property1 { get; set; }
public string Property2 { get; set; }
}
public class Model2
{
public int Property1 { get; set; }
public string Property2 { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
ModelContainer obj = new ModelContainer();
obj.Model1Property = new Model1() { Property1 = 1, Property2 = "some" };
obj.Model2Property = new Model1() { Property1 = 1, Property2 = "some" };
return View(obj);
}
查看
View
@model MyApp.Controllers.ModelContainer
<span> @Model.Model1Property.Property1</span>
参考: MVC单视图中的多个模型 [ ^ ]
这篇关于Mvc用两种模型显示视图中的数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!