这里是C#新手。
已选中“在视图控制器之间传递数据”,但是它有很多语法,我目前还不熟悉。计划在以后进行更多研究,例如协议和委托设计。
Root:制作一个ASP.NET应用程序。视图中从foreach填充的列表中的项无法填充到主控制器中:
查看语法
<input type="text" id="deviceId"/>
@Html.ActionLink("Add Device", "Add", new { /*id=item.PrimaryKey*/})
<table class="table">
<thead>
<tr>
<th>Include</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.include)
{
<tr>
<td>
<p>@Html.DisplayFor(m => item)</p> |
@Html.ActionLink("Exclude", "Exclude", new { /*id=item.PrimaryKey*/ }) |
==> <a asp-controller="Home" asp-action="Delete" asp-route-id="@item" class="btn btn-danger">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<table class="table">
<thead>
<tr>Exclude</tr>
</thead>
<tbody>
@foreach (var item in Model.exclude)
{
<tr>
<td>
<p>@Html.DisplayFor(m => item)</p> |
@Html.ActionLink("Include", "Include", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { })
</td>
</tr>
}
</tbody>
</table>
控制器删除方法
public IActionResult Delete(==>string id)
{
var theList = DeviceDictionaryConversion.DevDictionaryDEV();
theList.include.Remove(id);
return View("ListModDev", theList);
}
从视图中查看这是否是对象的正确数据类型,或者在这里我可能还没有完全理解其他什么想法。同样,来自示例运行的断点显示了控制器“ Delete”方法中的代码执行。
嗯,我不认为==>
是正确的语法....或该情况下的任何一种语法。
二:您在这里说的是当它被“单击”时
@Html.ActionLink("Delete", "Delete", new { })
调用Delete
方法。但是Delete
期待您未提供的id
参数。如果您没有传递任何内容,也没有必要执行此操作new { }
。
但是,如果您希望此方法不接受参数,也可以接受参数,则可以为该参数分配一个值,例如:
public IActionResult Delete(string id = "") // Set to empty string
您尝试使用<a asp-controller="Home" asp-action="Delete" asp-route-id="@item" class="btn btn-danger">Delete</a>
而不是@Url.Action()
吗?我发现这要简单得多,因为您需要做的只是:
@Url.Action("Delete", "Home", new {id = @item}) // Assuming that `@item` is a string
IE
<a href="@Url.Action("Delete", "Home", new {id = @item})" class="btn btn-danger">
因此错误似乎是用于处理Controller Delete的HTTP方法类型。
从POST切换到GET解决了该问题。