【问题标题】:how to delete a specific element of list in mvc?如何在mvc中删除列表的特定元素?
【发布时间】: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


【解决方案1】:

您必须将模型对象排除在操作方法之外,它需要可以从不同的方法中访问。

我会选择这样的:

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&lt;Trains&gt; 对象传递给构造函数参数?通常,这是由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");
    }
}

现在您有了一个对象,可以始终如一地存储您的火车列表,您可以用它做任何您想做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 2022-11-14
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多