如何在MVC中删除列表的特定元素?

时间:2018-08-29 11:57:24

标签: c# html asp.net razor model-view-controller

伙计,这是我的家庭控制器,我在其中声明了控制器并创建列表类和操作视图。

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>

现在我要删除列表中的一个元素并显示其余元素。我尝试了不同的方法,但整个列表都删除了,或者什么也没有删除。

1 个答案:

答案 0 :(得分:0)

您必须将模型对象置于action方法之外,需要从其他方法访问该对象。

我会选择这样的东西:

public class TrainManagerController : Controller
{
    // the train list is set at Controller level
    private List<Trains> TrainList;

    // and populated when the controller is instantiated
    public TrainManagerController(List<Trains> trainList)
    {
        TrainList = trainList;
    }

    // this calls the view with the train list you have at the moment
    public ViewResult Index()
    {
        return View(TrainList);
    }

    // this deletes the train, then calls the index view.
    public RedirectToRouteResult DeleteFromList(int TrainNumber)
    {
        TrainList.Remove(TrainList.FirstOrDefault(t => t.tno == TrainNumber));
        return RedirectToAction("Index");
    }
}

在视图中:

@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", "DeleteFromList", new {TrainNumber = item.tno})</td>
    </tr>
}

但是缺少一个片段:如何将List<Trains>对象传递给构造函数参数?通常,这是通过Dependency Injection完成的,在这种情况下-还实现了数据库连接,因此您需要一个接口(在这里将其称为ITrains),并且正确配置的第三方组件会为您填充列表

我知道该主题可能会让您头疼,因此为简洁起见,您可以跳过它,直到掌握了控制器和动作方法的工作原理,并以不同的方式构建该对象:

创建一个新的TrainContext类,该类将保留火车列表并提供处理该列表的方法(您需要删除,还记得吗?):

public class TrainContext
{
    private static List<Trains> trainList = null;

    public TrainContext()
    {
        if (trainList == null)
        {
            trainList = new List<Trains>();
            trainList.Add(new Trains() { tname = "vaigai", tno = 123, from = "Chennai", to = "trichy" });
            trainList.Add(new Trains() { tname = "express", tno = 456, from = "banglore", to = "chennai" });
        }
    }

    public List<Trains> GetTrains()
    {
        return trainList;
    }

    public void Delete(int TrainNumber)
    {
        if (trainList.Count > 0)
            trainList.Remove(trainList.FirstOrDefault(t => t.tno == TrainNumber));
    }
}

,以便您可以在任何时候请求TrainManager控制器时都可以调用该静态列表。现在我们必须相应地更改控制器

public class TrainManagerController : Controller
{
    private TrainContext context;

    public TrainManagerController()
    {
        context = new TrainContext();
    }

    public ViewResult Index()
    {
        return View(context.GetTrains());
    }

    public RedirectToRouteResult DeleteFromList(int TrainNumber)
    {
        context.Delete(TrainNumber);
        return RedirectToAction("Index");
    }
}

现在,您有了一个对象,该对象将始终存储火车列表,并且您可以执行任何操作。