通过实体,从视图到控制器 [英] Pass entity, from view to controller

查看:112
本文介绍了通过实体,从视图到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,这是我所

  @foreach(在Model.CommonMatches VAR匹配)
        {
            &所述; TR>                < TD> @ match.StartDateTime< / TD>
                < TD> @ match.EndDateTime< / TD>
                < TD> @ match.AvailableAttendees.Count()< / TD>
                < TD> @ Html.ActionLink(接受,AcceptAppointment,约会,新的{commonMatch = @match})LT; / TD>
            < / TR>
        }

Model.CommonMatches 的类型为列表与LT;窗GT;

 公共类窗口
{
    公众的DateTime的startDateTime {搞定;组; }
    公众的DateTime EndDateTime {搞定;组; }
    公共IEnumerable的< D​​ataModels.Attendee> AvailableAttendees {搞定;组; }}

这是怎样的价值正在从控制器通过

  [HTTPGET]
公众的ActionResult ViewStatus(GUID appointmentId)
{
    VAR状态=新ViewStatus
    {
        AttendeesWhoResponded = _appointmentRepository.GetAppointmentDetails(appointmentId).Attendees.Where(A => a.HasResponded == true)而.ToList()
        NotAttending = _appointmentRepository.GetAppointmentDetails(appointmentId).Attendees.Where(A => a.HasResponded ==真放;&安培; a.Responses == NULL).ToList()
        CommonMatches = _appointmentRepository.FindCommonMatches(appointmentId)
    };
    返回查看(状态);
}

ViewStatus类

 公共类ViewStatus
{
    公共ViewStatus()
    {
        AttendeesWhoResponded =新的List< D​​ataModels.Attendee>();
        NotAttending =新的List< D​​ataModels.Attendee>();
    }
    公开名单< D​​ataModels.Attendee> AttendeesWhoResponded {搞定;组; }    公开名单< D​​ataModels.Attendee> NotAttending {搞定;组; }
    公共IEnumerable的<窗​​GT; CommonMatches {搞定;组; }
}

在控制器的观点ActionLink的呼吁行动是:

  [HTTPGET]
公众的ActionResult AcceptAppointment(窗口commonMatch)
{
    返回内容(AC);
}

当我检查 commonMatch 在控制器的动作值。我越来越的startDateTime EndDateTime 但我没有得到所有的价值AvailableAttendees 。这是目前显示为

我很期待

AvailableAttendees类型为的IEnumerable<&与会者GT; 。是无法传递对象,我传递的方式吗?

我应该怎么做,也得到控制 AvailableAttendees 的所有值与日期一起?

编辑1:

 <表类=表悬停表条纹>
    <&THEAD GT;
        &所述; TR>
            < TD>开始时间与LT; / TD>
            < TD>结束时间< / TD>
            < TD>与会者和LT的数目; / TD>
        < / TR>
    < / THEAD>
            @for(VAR计数= 0; COUNT< Model.CommonMatches.Count();计数++)
            {
                使用(Html.BeginForm(AcceptAppointment,约会,FormMethod.Post))
                {
                    &所述; TR>
                        < TD> @ Model.CommonMatches [计数] .StartDateTime< / TD>
                        < TD> @ Model.CommonMatches [计数] .EndDateTime< / TD>
                        < TD> @ Model.CommonMatches [计数] .AvailableAttendees.Count()< / TD>
                        @ *< TD> @ Html.ActionLink(接受,AcceptAppointment,约会,新的{commonMatch = @match})LT; / TD> * @
                    @for(VAR J = 0; J< Model.CommonMatches [计数] .AvailableAttendees.Count(); J ++)
                    {
                        < TD> @ Model.CommonMatches [统计] .AvailableAttendees [J] .FirstName< / TD> //检查,如果该值为null或没有,只是一个测试
                        <td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].FirstName)</td>
                        <td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].LastName)</td>
                        &LT; TD&GT; @ Html.HiddenFor(M = GT; Model.CommonMatches [统计] .AvailableAttendees [J] .Email)LT; / TD&GT;
                        <td>@Html.HiddenFor(m=>Model.CommonMatches[count].AvailableAttendees[j].AttendeeId)</td>
                    }
                        &LT; TD&GT;&LT;输入类型=提交值=接受/&GT;&LT; / TD&GT;
                     &LT; / TR&GT;                }
            }
&LT; /表&gt;


解决方案

您需要张贴你的模型后面,这将涉及到更改控制器的方法,以这样的:

控制器

  [HttpPost]
公众的ActionResult AcceptAppointment(列表&LT;窗​​GT;模型)
{
    返回内容(AC);
}

查看

您查看需要一个表单和一个提交按钮,而不是一个 ActionLink的。我把表的格式出来以简化下面。

与索引你收藏的循环,使模型绑定知道如何处理它们,这其实是两个循环,因为它是一个集合中的一个集合。隐藏的值必须以太呈现给调回(请原谅任何的拼写错误)。

  @for(VAR I = 0; I&LT; Model.CommonMatches.Count;我++)
{
            &LT; D​​IV&GT;
                @using(Html.BeginForm(AcceptAppointment,约会,FormMethod.Post)
                {
                @ Html.HiddenFor(M = GT; Model.CommonMatches [I] .StartDateTime)
                @ Html.HiddenFor(M = GT; Model.CommonMatches [I] .EndDateTime)
                @ Model.CommonMatches [I] .StartDateTime&LT; BR /&GT;
                @ Model.CommonMatches [I] .EndDateTime&LT; BR /&GT;                @for(VAR J = 0; Model.CommonMatches [I] .AvailableAttendees.Count; J ++)
                {
                      @ Html.HiddenFor(M = GT; Model.CommonMatches [I] .AvailableAttendees [J] .Prop1)LT; BR /&GT;
                      @ Html.HiddenFor(M = GT; Model.CommonMatches [I] .AvailableAttendees [J] .Prop2)LT; BR /&GT;
                }                 &LT;输入类型=提交值=接受/&GT;
        &LT; / DIV&GT;
        }
}

In my view this is what I have

        @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 is of type List<Window>

public class Window
{
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }
    public IEnumerable<DataModels.Attendee> AvailableAttendees { get; set; }

}

This is how the value is being passed from 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 class

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

The action in controller that ActionLink of view is calling is:

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

When I inspect the value of commonMatch in controller's action. I'm getting the StartDateTime and EndDateTime but i'm not getting all the value of AvailableAttendees. It is currently shown as null.

AvailableAttendees that I'm expecting is of type IEnumerable<Attendee>. Is is not possible to pass the object the way I'm passing?

What should I do, to also get all the values of AvailableAttendees in controller along with dates?

Edit 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>

解决方案

You need to post your model back, this would involve changing your controller method to this:

Controller

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

View

You view would need a form and a submit button rather than an ActionLink. I have take the table formatting out to simplify the below.

Indexing your collections with a for loop so the model binder knows how to handle them, this is actually two loops as it is a collection within a collection. The hidden values have to be rendered too in order to be posted back (please forgive any typos).

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

这篇关于通过实体,从视图到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆