当我按下删除视图时,MVC不显示模型的数据

时间:2016-12-02 10:05:35

标签: asp.net asp.net-mvc asp.net-mvc-5

我有这段代码

|Year  | customer_id |
|2010  | 1           |
|2010  | 2           |
|2011  | 2           |
|2011  | 3           |
|2012  | 1           |

和这个HTML

[HttpGet]
        public ActionResult DeleteKamion(int id)
        {
            using (TruckCompanyEntities1 databaseKamion2 = new TruckCompanyEntities1())
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                Kamioni KamionDelete = databaseKamion2.Kamionis.Find(id);

                if (KamionDelete == null)
                {
                    return HttpNotFound();
                }

                return View();
            }
        }

        [HttpPost, ActionName("DeleteKamion")]
        public ActionResult DeleteKamionConfirmed(int id)
        {
            using (TruckCompanyEntities1 databaseKamion10 = new TruckCompanyEntities1())
            {
                Kamioni SoferDeleteConfirmed = databaseKamion10.Kamionis.Find(id);
                databaseKamion10.Kamionis.Remove(SoferDeleteConfirmed);
                databaseKamion10.SaveChanges();

                return RedirectToAction("DisplayUserDetailsKamion");

            }


        }

问题是,当我按下DELETE视图时没有显示模型的值,但是当我按下删除按钮时,我也想显示信息,请帮助谢谢。

1 个答案:

答案 0 :(得分:2)

这是因为你正在返回视图emtpy ..视图正在期待一个模型。提取数据但不返回。

[HttpGet]
public ActionResult DeleteKamion(int id)
{
    using (TruckCompanyEntities1 databaseKamion2 = new TruckCompanyEntities1())
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Kamioni KamionDelete = databaseKamion2.Kamionis.Find(id);

        if (KamionDelete == null)
        {
            return HttpNotFound();
        }

        return View(KamionDelete);
    }
}