【发布时间】:2019-02-04 05:01:31
【问题描述】:
伙计们,这是我的家庭控制器,我在其中声明控制器并创建列表类和操作视图。
namespace MvcApplication6.Controllers
{
public class Trains
{
public string tname { get; set; }
public int tno { get; set; }
public string from { get; set; }
public string to { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.message = "Welcome to trains list!";
return View();
}
public ActionResult About()
{
List<Trains> details = new List<Trains>() { };
details.Add(new Trains() { tname = "vaigai", tno = 123, from = "Chennai", to = "trichy" });
details.Add(new Trains() { tname = "express", tno = 456, from = "banglore", to = "chennai" });
return View(details);
}
这是我的html页面,我让控制器在表格中一一显示列表
@{
ViewBag.Title = "delete";
}
@{
ViewBag.Title = "Available Trains";
}
@model List<MvcApplication6.Controllers.Trains>
<h2>list of available trains are:</h2>
<p>
<table>
<tr>
<th>TRAIN NAME</th>
<th>TRAIN NUMBER</th>
<th>FROM</th>
<th>TO</th>
<th>Delete</th>
</tr>
@foreach (var item in @Model)
{
<tr style="color: blue">
<td>@item.tname</td>
<td>@item.tno</td>
<td>@item.from</td>
<td>@item.to</td>
<td> @Html.ActionLink("Delete","About",new{i = item.tno})</td>
</tr>
}
</table>
现在我想删除列表中的一个元素并显示剩余的元素。我尝试了不同的方法,但整个列表删除或没有被删除。
【问题讨论】:
-
i tried different methods向我们展示这些尝试。 -
你要删除什么元素?你能告诉我们你到目前为止做了什么
-
为什么不使用数据库和存储项目而不是使用静态列表?
-
@SusenMaharjan 为什么?这对他的问题有什么改变吗?他可能只是测试东西,不需要数据库自动取款机。
-
您必须将 details 对象放在 action 方法之外,以便可以从删除方法中查看它,该方法可以实现一个简单的 Linq 查询来删除选定的项目
标签: c# html asp.net razor model-view-controller