【问题标题】:Throw message after successfully insert into database or error message if not successful成功插入数据库后抛出消息,如果不成功则抛出错误消息
【发布时间】:2019-01-25 07:34:04
【问题描述】:

我正在插入我的数据库。我的代码工作正常,但是如果插入成功与否,我会试图抛出消息。请在下面检查我的代码。例如,如果插入成功,则应显示“插入成功”之类的消息。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        SalesLayanEntities3 db = new SalesLayanEntities3();
        List<Product_Category> list = db.Product_Category.ToList();
        ViewBag.ProductName = new SelectList(list,"cat_id","cat_name");

        return View();
    }

    public ActionResult SaveRecord(ProductForm model)
    {
        try
        {
            SalesLayanEntities3 db = new SalesLayanEntities3();
            Product prod = new Product();

            prod.prod_name = model.Prod_name;
            prod.prod_model = model.Prod_model;
            prod.prod_quantity = model.Prod_quantity;
            prod.prod_description = model.Prod_description;
            prod.prod_unit_cost_price = model.Prod_unit_cost_price;
            prod.cat_id = model.Cat_id;

            db.Products.Add(prod);
            db.SaveChanges();
            int latestProdId = prod.prod_id;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return RedirectToAction("Index");
    }
 }

【问题讨论】:

  • 请注意,visual-studio 标记用于解决有关 Visual Studio 应用程序的问题。另外,如果您使用的是实体框架,请为其添加标签。
  • 嗨,我认为您必须查看带参数的 redirectToAction 看看这个stackoverflow.com/questions/1257482/…
  • 旁注:你应该删除你的 try/catch 块没有增加价值(或者如果你认为它增加了价值,至少让它 throw 而不是 throw ex;
  • 我通过在控制器内部创建新视图并重定向到页面并插入完成来找到解决方案。但是请问有没有办法在控制器内部使用“if语句”来返回成功消息?

标签: c# sql-server


【解决方案1】:

您没有收到任何消息,因为您正在重定向到索引页面。

您可能希望重定向到成功页面RedirectToAction("Success", "Shared")

你可以使用错误页面来抛出异常,我相信你已经默认这样做了。

【讨论】:

【解决方案2】:

您可能想要使用 TempData。它可以在您的视图中访问。

控制器:

TempData["status"] = "Success";

查看:

{
    @TempData["status"];
}

你可以这样做:

public ActionResult SaveRecord(ProductForm model)
{
    try
    {
        SalesLayanEntities3 db = new SalesLayanEntities3();
        Product prod = new Product();

        prod.prod_name = model.Prod_name;
        prod.prod_model = model.Prod_model;
        prod.prod_quantity = model.Prod_quantity;
        prod.prod_description = model.Prod_description;
        prod.prod_unit_cost_price = model.Prod_unit_cost_price;
        prod.cat_id = model.Cat_id;

        db.Products.Add(prod);
        db.SaveChanges();
        int latestProdId = prod.prod_id;
        TempData["status"] = "Success";
    }
    catch (Exception ex)
    {
        TempData["status"] = "Error";
        throw ex;
    }
    return RedirectToAction("Index");
}

【讨论】:

  • 但是我必须返回一些东西,因为我的方法不是无效的
  • TempData 可以与您的return RedirectToAction("Index");配对
【解决方案3】:

尝试在redirect 中添加一个参数(您需要更改latestProdId 的范围):

return RedirectToAction("Index", new {addedID = latestProdId );

然后给Index添加一个可选参数

public ActionResult Index(int latestProdId  = 0)

测试latestProdId 的非零值,然后将其传递回视图以显示。

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 2015-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多