传递实体,从视图到控制器

在我看来,这就是我所拥有的

@foreach (var match in Model.CommonMatches) {  @match.StartDateTime @match.EndDateTime @match.AvailableAttendees.Count() @Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new {commonMatch = @match })  } 

Model.CommonMatches的类型为List

 public class Window { public DateTime StartDateTime { get; set; } public DateTime EndDateTime { get; set; } public IEnumerable 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(); NotAttending = new List(); } public List AttendeesWhoResponded { get; set; } public List NotAttending { get; set; } public IEnumerable CommonMatches { get; set; } } 

视图的ActionLink调用的控制器中的操作是:

 [HttpGet] public ActionResult AcceptAppointment(Window commonMatch) { return Content("ac"); } 

当我在控制器的动作中检查commonMatch的值时。 我得到了StartDateTimeEndDateTime但我没有得到AvailableAttendees全部价值。 它目前显示为null

我期待的AvailableAttendees是IEnumerable类型。 是不是可以按照我传递的方式传递对象?

我该怎么做,还要在控制器中获取AvailableAttendees所有值以及日期?

编辑1:

  @for (var count = 0; count < Model.CommonMatches.Count();count++ ) { using (Html.BeginForm("AcceptAppointment", "Appointment", FormMethod.Post)) {  @**@ @for(var j=0;j<Model.CommonMatches[count].AvailableAttendees.Count();j++) { //to check if the value is null or not, just a test  }  } } 
Start time End time Number of Attendees
@Model.CommonMatches[count].StartDateTime @Model.CommonMatches[count].EndDateTime @Model.CommonMatches[count].AvailableAttendees.Count()@Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new { commonMatch = @match })@Model.CommonMatches[count].AvailableAttendees[j].FirstName@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].FirstName) @Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].LastName) @Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].Email) @Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].AttendeeId)

您需要将模型发回,这将涉及将控制器方法更改为:

调节器

 [HttpPost] public ActionResult AcceptAppointment(List model) { return Content("ac"); } 

视图

您需要一个表单和一个提交按钮而不是ActionLink 。 我已将表格格式化为简化以下内容。

使用for循环索引集合,以便模型绑定器知道如何处理它们,这实际上是两个循环,因为它是集合中的集合。 隐藏的值也必须被渲染回来(请原谅任何错别字)。

 @for(var i = 0; i < Model.CommonMatches.Count; i ++) { 
@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
@Model.CommonMatches[i].EndDateTime
@for(var j = 0; Model.CommonMatches[i].AvailableAttendees.Count; j++) { @Html.HiddenFor(m => Model.CommonMatches[i].AvailableAttendees[j].Prop1)
@Html.HiddenFor(m => Model.CommonMatches[i].AvailableAttendees[j].Prop2)
}
} }

你需要照顾很多东西

 @Html.ActionLink("Accept", "AcceptAppointment", "Appointment", new {commonMatch = @match }) 

呼叫

 [HttpGet] public ActionResult AcceptAppointment(Window commonMatch) { return Content("ac"); } 

在这里,您使用链接进行导航。 基本上你发出了一个get请求。 在get请求中,您只能通过Query String将数据传递给服务器。 但是你的情况是,在导航到url之前动态准备一个查询字符串有点复杂。 但你可以用像onclick=prepareHref(this)这样的JavaScript来做到这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; } 

但这不是建议的做法。

如果你想打开其他页面并显示url,最好再次传递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"); }