Skip to main content
 首页 » 编程设计

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

2024年02月13日11sky-heaven

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

        @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 中。我得到 StartDateTime和 EndDateTime但我没有得到 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> 
        } 
}