c# - 传递实体,从 View 到 Controller

标签 c# javascript jquery asp.net asp.net-mvc

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

        @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; }

}

这是从 Controller 传递值的方式

[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 调用的 Controller 中的 Action 是:

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

当我检查 commonMatch 的值时在 Controller 的 Action 中。我得到 StartDateTimeEndDateTime但我没有得到 AvailableAttendees 的所有值(value).当前显示为 null .

我期望的 AvailableAttendees 类型为 IEnumerable<Attendee> .不可能按照我传递的方式传递对象吗?

我应该怎么做,才能同时获取 AvailableAttendees 的所有值?在 Controller 中连同日期?

编辑 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>

最佳答案

您需要将您的模型发回,这将涉及将您的 Controller 方法更改为:

Controller

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

查看

您的 View 需要一个表单和一个提交按钮,而不是 ActionLink。我已删除表格格式以简化下面的内容。

使用 for 循环索引您的集合,以便模型绑定(bind)器知道如何处理它们,这实际上是两个循环,因为它是集合中的集合。隐藏的值也必须呈现才能发回(请原谅任何拼写错误)。

@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>
        }
}

关于c# - 传递实体,从 View 到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013660/

相关文章:

c# - 如何以编程方式将站点固定到 Windows 8 中的启动屏幕

c# - 将 DbSet<Entity> 转换为其 DbSet<IEntity> 而无需将所有实体拉入内存

c# - 用于桌面应用程序的 Java Swing 或 Windows 窗体?

javascript - 在 JavaScript 中解构对象时如何绑定(bind)方法?

javascript - IE8 中未定义的 JQuery

javascript - 如果日期尚未显示,如何仅将日期附加到聊天中?

c# - 如何停止在表单关闭时触发文本框离开事件

javascript - KnockoutJS 复选框列表不绑定(bind)所选值

javascript - 我正在尝试使用 javascript 将图像添加到 HTML 但没有内容?

javascript - 如何使用javascript在输入元素中显示特殊字符?