【发布时间】:2014-08-18 23:04:14
【问题描述】:
我有一个 VIEW,它将 ViewData 中的数据呈现到表格中,如下所示。
如您所见,每一行都有一个按钮。
我想在单击时将 Row 数据发送到相应按钮的控制器。
我如何做到这一点?
请提供建议和最佳实施方法。
我尝试使用敲除绑定此数据并将其发布到控制器,正如我在此 How to Bind data from Razor to Knockout? 中发布的那样,它不起作用,没有任何绑定。
查看
@{
var UserWRInfo = (List<InfoEntity>)ViewData["UserWRInfo"];
var UserOwner = (List<string>)ViewData["UserOwner"];
<table class="table table-bordered">
<thead>
<tr>
<th ><b>Status</b></th>
<th ><b>Appropriation</b></th>
<th ><b>PriorityDate</b></th>
<th ><b>Location</b></th>
<th ><b>Source</b></th>
<th ><b>Owner</b></th>
<th ><b>Use</b></th>
<th ><b>StartedBy</b></th>
<th ><b>RequiredReporting</b></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < UserWRInfo.Count; i++)
{
<tr>
<td><button type="submit" class="btn btn-primary">Start</button></td>
<td> @UserWRInfo[i].AppropriationNumber</td>
<td> @UserWRInfo[i].PriorityDate.ToString("MM/dd/yyyy")</td>
<td> @UserWRInfo[i].Sect @UserWRInfo[i].Township @UserWRInfo[i].Range@UserWRInfo[i].RangeDirectionID</td>
<td> @UserWRInfo[i].Source</td>
@if (UserWRInfo.Count == UserOwner.Count)
{
<td> @UserOwner[i].ToString()</td>
}
else
{
<td ></td>
}
<td> @UserWRInfo[i].UseDescription</td>
<td></td>
<td> @UserWRInfo[i].isAnnualReportRequired</td>
</tr>
}
</tbody>
</table>
}
【问题讨论】:
-
您使用 ViewData 而不是绑定到模型有什么特别的原因吗?
-
由于这是一个开放式问题,对于初学者,请尝试删除所有内联样式,这将使代码更具可读性并消除您的顾虑。
-
没什么特别的,我发现这是获取模型对象列表的一种简单方法。
-
你想使用 AJAX 来实现这一点还是你不介意?
-
我刚刚更新了这个问题,提供了一个链接,指向我如何尝试使用淘汰赛 js 绑定数据并将其发送到无效的控制器。
标签: c# asp.net-mvc razor knockout.js