在我看来,这就是我所拥有的
@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; }
}
视图的ActionLink调用的控制器中的操作是:
[HttpGet]
public ActionResult AcceptAppointment(Window commonMatch)
{
return Content("ac");
}
当我在控制器的操作中检查commonMatch
的值时。我收到StartDateTime
和EndDateTime
,但我没有得到AvailableAttendees
的全部价值。目前显示为null
。
我期待的AvailableAttendees类型为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>
答案 0 :(得分:2)
您需要将模型发回,这将涉及将控制器方法更改为:
<强>控制器强>
[HttpPost]
public ActionResult AcceptAppointment(List<Window> model)
{
return Content("ac");
}
查看强>
您需要一个表单和一个提交按钮而不是ActionLink
。我已将表格格式化为简化以下内容。
使用for循环索引集合,以便模型绑定器知道如何处理它们,这实际上是两个循环,因为它是集合中的集合。隐藏的值也必须被渲染回来(请原谅任何错别字)。
@for(var i = 0; i < Model.CommonMatches.Count; i ++)
{
<div>
@using (Html.BeginForm("AcceptAppointment", "Appointment", FormMethod.Post)
{
@Html.HiddenFor(m => Model.CommonMatches[i].StartDateTime)
@Html.HiddenFor(m => Model.CommonMatches[i].EndDateTime)
@Model.CommonMatches[i].StartDateTime <br/>
@Model.CommonMatches[i].EndDateTime <br/>
@for(var j = 0; Model.CommonMatches[i].AvailableAttendees.Count; j++)
{
@Html.HiddenFor(m => Model.CommonMatches[i].AvailableAttendees[j].Prop1)<br/>
@Html.HiddenFor(m => Model.CommonMatches[i].AvailableAttendees[j].Prop2)<br/>
}
<input type="submit" value="Accept" />
</div>
}
}
答案 1 :(得分:2)
你需要照顾很多事情
<td>@Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new {commonMatch = @match })</td>
呼叫
[HttpGet]
public ActionResult AcceptAppointment(Window commonMatch)
{
return Content("ac");
}
您在此处使用链接<a href>
进行导航。基本上你发出了一个get请求。在get请求中,您只能通过Query String
将数据传递到服务器。但是你的情况是,在导航到url之前动态准备一个查询字符串有点复杂。但是你可以使用像onclick=prepareHref(this)
;
@Html.ActionLink("Accept", "AcceptAppointment", "Appointment",
new {commonMatch = @match }, new {onclick=prepareHref(this)})
然后在Javascript中
function prepareHref(obj)
{
var qsData="?StartDateTime='2014-02-25'&EndDateTime='2014-02-25'&AvailableAttendees[0].prop1=value1, etc"; // data should be obtained from other td elements
obj.href=obj.href+qsData;
}
但这并不是建议的做法。
如果你想打开其他页面并显示网址,最好再次传递id并加载数据。
选项1:
更好的方法是在@hutchonoid解释隐藏字段中提交细节。
选项2:
或者在jQuery ajax $.post
方法中提交详细信息。您需要使用POST
@Html.ActionLink("Accept", "AcceptAppointment", "Appointment",
new {commonMatch = @match }, new {onclick=postMyData()})
function postMyData(){
var postData={};
postData.StartDateTime='';
postData.EndDateTime='';
postData.AvailableAttendees=[];
//for each AvailableAttendees prepare object
postData.AvailableAttendees[0]= {};
postData.AvailableAttendees[0].prop1=value1;
$.post('/Appointment/AcceptAppointment/',{data:postData},function(data){
});
return false;
}
[HttpPost]
public ActionResult AcceptAppointment(Window commonMatch)
{
return Content("ac");
}