我有一个复杂的View
。它包含4 Models
的数据。模型都是静态的,并按预期工作。我创建了一个ViewModel来尝试只显示该视图所需的数据。它由竞争对手和他们参与的一些复杂的课程和活动组成。
我制作了一个复杂的ViewModel
。当我走过Controller
时,我可以看到所有三个部分都是从ViewModel
构建的。其中包括数据。当我尝试使用Intellesense
中的View
映射值时,它无法知道此数据,或者没有来自复杂ViewModel的映射。我这样做了吗?我尝试了几种方法将这些值映射到View
。我想我需要initialize
或map
来自Models
的值,我只是无法弄清楚如何。
请告知如何将这些值,数据元素映射到视图。
视图模型:
Compeditor
是直接到DB的实际模型
其余数据从多个表中收集并传递给控制器视图
namespace eManager.Web2.Models
{
public class CompDetailPlus
{
public CompDetailPlus()
{
this.Compeditor = new Compeditor();
}
public virtual Compeditor Compeditor { get; set; }
public virtual IEnumerable<InEventClass> InEventClass { get; set; }
public virtual IEnumerable<AllEventClasses> AllEventClasses { get; set; }
}
public class Compeditor
{
[Key]
public virtual int CompeditorId { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string MiddleInt { get; set; }
public virtual string StreetAddress { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string PostalCode { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string HomePhone { get; set; }
public virtual string CellPhone { get; set; }
public virtual double Height { get; set; }
public virtual double Weight { get; set; }
public virtual int Age { get; set; }
public virtual int Event_CompId { get; set; }
}
public class InEventClass
{
public virtual int EventClassID { get; set; }
public virtual string ClassName { get; set; }
public virtual bool IsSelected { get; set; }
}
//duplicate to simplify how the second list is pulled and then combined with first list
public class AllEventClasses
{
public virtual int EventClassID { get; set; }
public virtual string ClassName { get; set; }
public virtual bool IsSelected { get; set; }
}
}
控制器:
public ActionResult CompeditorDetail(int CompeditorId)
{
//Pull the Competitor detail for the ID passed in
var comp = _db.Compeditors.Single(c => c.CompeditorId == CompeditorId);
//Pull a list of Event-Classes the competitor is already signed up for on current event
var nlist = (from o in _db.Compeditors
join o2 in _db.Event_Class_Compeditors_s on o.CompeditorId equals CompeditorId
where o.CompeditorId.Equals(CompeditorId)
join o3 in _db.Event_Classes on o2.EventClassID equals o3.EventClassID
where o2.EventClassID.Equals(o3.EventClassID)
join o4 in _db.Class_Definitions on o3.ClassID equals o4.Class_Definition_ID
where o3.ClassID.Equals(o4.Class_Definition_ID)
select new InEventClass()
{
ClassName = o4.Class_Name,
EventClassID = o2.EventClassID,
IsSelected = true
}).ToList();
//pull a complete list of Event Classes that are avaiaible
var totallist = (from o in _db.Event_Classes
join o2 in _db.Event_Classes on o.ClassID equals o2.ClassID
where o.ClassID.Equals(o2.ClassID)
join o3 in _db.Class_Definitions on o2.ClassID equals o3.Class_Definition_ID
where o2.ClassID.Equals(o3.Class_Definition_ID)
join o4 in _db.Events on o.EventID equals o4.EventID
where o.EventID.Equals(o4.EventID)
where o4.CurrentEvent.Equals(true)
select new AllEventClasses()
{
ClassName = o3.Class_Name,
EventClassID = o2.EventClassID,
IsSelected = false
}).ToList();
var whatsleft = totallist.Where(eachtotalclass => !(nlist.Any(eachClassIHave => eachClassIHave.EventClassID == eachtotalclass.EventClassID))).ToList();
var model = new CompDetailPlus { AllEventClasses = whatsleft, Compeditor = comp, InEventClass = nlist };
return View(model);
}
查看:
(必须显示竞争对手的详细信息以及他们所在的Event_Classes的复合列表)
在视图中,我看不到任何数据的值..运行中的所有错误,没有显示的好处。
@model IEnumerable<eManager.Web2.Models.CompDetailPlus>
@{
ViewBag.Title = "Competitor's Detail";
}
<h2>@ViewBag.Title</h2>
<fieldset>
<legend>Compeditor</legend>
<table border="1" >
<tr>
<td>
<div class="display-field">
@Html.HiddenFor(model => model.Compeditor.CompeditorId)
</div>
<b>First Name</b>
<div class="display-field">
@Html.DisplayFor(model => model.Compeditor.FirstName)
</div>
</td>
<td>
<b>Last Name</b>
<div class="display-field">
@Html.DisplayFor(model => model.Compeditor.LastName)
</div>
</td>
@using (Html.BeginForm("CompeditorDetail", "Compeditor", FormMethod.Post))
{
foreach (var item in Model)
{
<input type="checkbox" name="MyID" value="@item.AllEventClasses.IsSelected"/> @item.InEventClass.ClassName <br />
<input type="hidden" name="CompeditorID" value="@item.InEventClass.CompeditorId" />
}
}
</td>
答案 0 :(得分:1)
您的View接受IEnumerable eManager.Web2.Models.CompDetailPlus的模型,这样会很好,但您的控制器正在发送一个eManager.Web2.Models.CompDetailPlus对象。
尝试在视图中更改此内容
@model IEnumerable<eManager.Web2.Models.CompDetailPlus>
到此:
@model eManager.Web2.Models.CompDetailPlus
并更改视图的底部,以便它遍历模型中的Enumerable compaosite项目。