数据不传递给控制器 [英] Data Not Pass to Controller
问题描述
我正在显示数据列表,点击提交按钮,数据应该转到特定控制器,但是我不能这样做。以下是我正在显示数据的部分视图:
I am displaying list of data now on click of submit button the data should go to the Specific Controller but I am not able to do that. Below is the Partial View on which I am displaying data:
@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")'" />*@
}
以下数据显示的控制器如下:
The controller from which above data is display is given below:
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);
}
现在我只想将该部分数据发送到特定控制器,但是数据不到:
Now here I just want to send that Partial data to a Specific controller but that data not comes:
[HttpPost]
public ActionResult SubmitHours(List<AccMng_TicketDetails> _AccMng_TicketDetails)
{
return View(_AccMng_TicketDetails);
}
推荐答案
@Leonardo说,最少使用List而不是IEnumerable作为模型定义来使用基于索引的方法。如果您只想回传每行的注释,请将注释框更改为基于表达式的帮助器,并为GUID添加隐藏字段(因此将其发回控制器)。
First, I would recommend at least use List instead of IEnumerable as your model definition to use the index-based approach, as @Leonardo said. If you are just trying to only pass back the comments for each row, change the comment box to an expression-based helper, and add a hidden field for the GUID (so it posts back to the controller).
@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回发一个对象列表,仅填写评论字段(因为它们是用户界面中的输入)。
When the page posts back, it will postback a list of objects with TicketDetailsGuid and Comment fields populated only (because they are inputs in the UI).
这篇关于数据不传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!