数据未传递给控制器

时间:2017-07-20 10:43:13

标签: sql asp.net-mvc entity-framework

我现在点击提交按钮显示数据列表,数据应该转到特定控制器,但我无法做到。下面是我正在显示数据的部分视图:

@model IEnumerable<WebApplication1.Models.TicketDetails>

@using (Html.BeginForm("SubmitHours", "TicketDetails", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    if (Model.Count() > 0)
    {
        <hr />
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>Ticket No.</th>
                <th>Summary</th>
                <th>Status</th>
                <th>Action Date</th>
                <th>Billable Hours</th>
            </tr>
            @foreach (var ticketdata in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelitem => ticketdata.TicketDetailsGuid)</td>
                    <td>@Html.DisplayFor(modelitem => ticketdata.Summary)</td>
                    <td>@Html.DisplayFor(modelitem => ticketdata.CAStatus)</td>
                    <td>@Html.DisplayFor(modelitem => ticketdata.ActionDate)</td>
                    <td>@Html.TextBox("Comment", null, new { @class = "form-control" })  </td>
                </tr>
            }

        </table>

    }
    <input type="submit" value="Submit" />
    @*<input type="button" value="Submit" onclick="location.href='@Url.Action("SubmitHours", "TicketDetails")'" />*@
}

显示上述数据的控制器如下:

public ActionResult Index()
        {
            List<TicketDetails> _TicketDetails = new List<TicketDetails>();
            _TicketDetails = (from ticketdata in _entities.AccMng_TicketDetails.AsEnumerable()
                                     select new TicketDetails
                                     {TicketDetailsGuid = ticketdata.TicketDetailsGuid,
                                      ActionBy = ticketdata.ActionBy,
                                     CAStatus = ticketdata.CAStatus,
                                     Comment = ticketdata.Comment,
                                     Summary = ticketdata.Summary}).ToList();

            return View(_TicketDetails);
        }

现在,我只想将该部分数据发送给特定控制器,但数据不会出现:

        [HttpPost]
        public ActionResult SubmitHours(List<AccMng_TicketDetails> _AccMng_TicketDetails)
        {
            return View(_AccMng_TicketDetails);
        }

2 个答案:

答案 0 :(得分:3)

首先,我建议至少使用List而不是IEnumerable作为模型定义来使用基于索引的方法,正如@Leonardo所说。如果您只是尝试仅传回每行的注释,请将注释框更改为基于表达式的帮助程序,并为GUID添加隐藏字段(以便将其发回给控制器)​​。

@for (var i = 0; i < Model.Count; i++)
{
     <tr>
       <td>
         @Html.DisplayFor(m => m[i].TicketDetailsGuid)
         @Html.HiddenFor(m => m[i].TicketDetailsGuid)
       </td>
       <td>@Html.DisplayFor(m => m[i].Summary)</td>
       <td>@Html.DisplayFor(m => m[i].CAStatus)</td>
       <td>@Html.DisplayFor(m => m[i].ActionDate)</td>
       <td>@Html.TextBoxFor(m => m[i].Comment, new { @class = "form-control" })  </td>
     </tr>
}

当页面回发时,它将回发一个仅包含TicketDetailsGuid和Comment字段的对象列表(因为它们是UI中的输入)。

答案 1 :(得分:0)

尝试使用@EditorFor代替DisplayFor和@for而不是@foreach并引用任何带有索引的项目

@Html.EditorFor(model => model[i].Summary)