0

我目前正在 MVC 3 中做一个项目,无法确定用户是否传递了无效的 id(比如说 23233),我如何向用户显示具有此 id 的项目不存在的消息?

4

2 回答 2

1

假设这是 ASP.NET,请使用Find()DbSetId. 如果结果是null,请使用类似RedirectToAction()将用户发送到解释问题的页面。

VS 脚手架系统已经做了类似的事情,除了它HttpNotFound()在自动生成的代码中返回一个代替。您可以使用它的逻辑作为起点。

于 2013-04-30T06:49:22.697 回答
0

第一的。您正在为 id 创建一个检查器方法。

 public bool idChecker(string id)
    {
        try
        {
            double numeric = -1;
            bool retval = double.TryParse(id, out numeric);
            return retval;
        }
        catch (Exception)
        {
            return false;
        }

    }

您将使用 idChecker 方法。

        public ActionResult YourActionMethod(string id)
    {
        if (!idChecker(id))
            return Content("Invalid ID"); // or your code
        else
            return View(); // or your code.
    }
于 2013-04-30T08:57:56.770 回答