Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个动作方法连接到一个视图_C#_Asp.net Mvc - Fatal编程技术网

C# 将多个动作方法连接到一个视图

C# 将多个动作方法连接到一个视图,c#,asp.net-mvc,C#,Asp.net Mvc,我面临的问题是,当单击“删除”链接时,我的“删除操作结果”被称为“错误”,表示没有删除视图。我曾尝试设置ActionName(“索引”),但随后收到一条关于视图索引的模糊错误消息 public ActionResult Index() { ***code goes here**** return View(viewModel); } [HttpPost, ActionName("Index")]

我面临的问题是,当单击“删除”链接时,我的“删除操作结果”被称为“错误”,表示没有删除视图。我曾尝试设置ActionName(“索引”),但随后收到一条关于视图索引的模糊错误消息

 public ActionResult Index()
        {
            ***code goes here****

            return View(viewModel);
        }

        [HttpPost, ActionName("Index")]
        [OnAction(ButtonName = "Create")]
        public ActionResult Index(***code goes here***)
        {

            ***code goes here****


            return RedirectToAction("Index");
        }

        //GET:
        public ActionResult Delete(int? id)
        {
            ***code goes here****

            return View(lansing);
        }

        //POST:
        [HttpPost, ActionName("Index")]
        [OnAction(ButtonName = "Delete")]
        //[ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            ***code goes here****

            return RedirectToAction("Index");
        }

然后,我有一个表,该表使用用户输入的信息进行更新,并在从表中单击actionLink时删除信息,该表由这两个ActionResults处理。

您可以通过将视图的名称作为第一个参数传递给
view
方法的调用来指定要呈现的视图(参见或的文档)


但是由于
httpget Delete
方法通常用于向用户显示要删除的元素,并提示用户确认删除(参见此),您可能想为它添加一个专用视图。

我面临的问题的解决方案是在Get Delete方法中实际执行Delete Post的操作,因为创建、编辑和删除都使用一个视图

最终结果如下:

  public ActionResult Delete(int? id)
        {
            ***code goes here****

           var lansing = db.LansingMileages.Find(id);
            viewModel.Records = new[]
            {
                lansing
            };

            db.LansingMileages.Remove(lansing);
            db.SaveChanges();
            return RedirectToAction("Index");
        }