【发布时间】:2014-02-25 12:04:22
【问题描述】:
在我看来,这就是我所拥有的
@foreach (var match in Model.CommonMatches)
{
<tr>
<td>@match.StartDateTime</td>
<td>@match.EndDateTime</td>
<td>@match.AvailableAttendees.Count()</td>
<td>@Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new {commonMatch = @match })</td>
</tr>
}
Model.CommonMatches 的类型为 List<Window>
public class Window
{
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public IEnumerable<DataModels.Attendee> AvailableAttendees { get; set; }
}
这是从控制器传递值的方式
[HttpGet]
public ActionResult ViewStatus(Guid appointmentId)
{
var status = new ViewStatus
{
AttendeesWhoResponded = _appointmentRepository.GetAppointmentDetails(appointmentId).Attendees.Where(a=>a.HasResponded == true).ToList(),
NotAttending = _appointmentRepository.GetAppointmentDetails(appointmentId).Attendees.Where(a=>a.HasResponded == true && a.Responses == null).ToList(),
CommonMatches = _appointmentRepository.FindCommonMatches(appointmentId)
};
return View(status);
}
ViewStatus 类
public class ViewStatus
{
public ViewStatus()
{
AttendeesWhoResponded = new List<DataModels.Attendee>();
NotAttending = new List<DataModels.Attendee>();
}
public List<DataModels.Attendee> AttendeesWhoResponded { get; set; }
public List<DataModels.Attendee> NotAttending { get; set; }
public IEnumerable<Window> CommonMatches { get; set; }
}
view的ActionLink正在调用的控制器中的动作是:
[HttpGet]
public ActionResult AcceptAppointment(Window commonMatch)
{
return Content("ac");
}
当我在控制器的操作中检查commonMatch 的值时。我得到了StartDateTime 和EndDateTime,但我没有得到AvailableAttendees 的所有价值。目前显示为null。
我期待的可用参加者是IEnumerable<Attendee> 类型。是不是不能以我传递的方式传递对象?
我应该怎么做才能在控制器中同时获取 AvailableAttendees 的所有值以及日期?
编辑 1:
<table class ="table-hover table-striped">
<thead>
<tr>
<td>Start time</td>
<td>End time</td>
<td>Number of Attendees</td>
</tr>
</thead>
@for (var count = 0; count < Model.CommonMatches.Count();count++ )
{
using (Html.BeginForm("AcceptAppointment", "Appointment", FormMethod.Post))
{
<tr>
<td>@Model.CommonMatches[count].StartDateTime</td>
<td>@Model.CommonMatches[count].EndDateTime</td>
<td>@Model.CommonMatches[count].AvailableAttendees.Count()</td>
@*<td>@Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new { commonMatch = @match })</td>*@
@for(var j=0;j<Model.CommonMatches[count].AvailableAttendees.Count();j++)
{
<td>@Model.CommonMatches[count].AvailableAttendees[j].FirstName</td>//to check if the value is null or not, just a test
<td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].FirstName)</td>
<td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].LastName)</td>
<td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].Email)</td>
<td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].AttendeeId)</td>
}
<td><input type="submit" value="Accept"/></td>
</tr>
}
}
</table>
【问题讨论】:
-
你是如何从控制器传递模型的?
-
@Andrei 有问题地添加了它。所有值都传递给视图。我可以在浏览器中看到值。
-
如果您没有包含 availableAttendee 但想使用延迟加载,您应该将 virtual 添加到 IEnumerable
-
@S4NDERR 在将对象从视图传递到控制器时是否也有效?
-
@Biplov13 我想我现在明白你的问题了,类似的问题在这里:stackoverflow.com/questions/16321736/…
标签: c# javascript jquery asp.net asp.net-mvc