1

I am inserting into my database. My code is working fine, but am stuck trying to throw message if the insertion is successful or not. Please check my code below. For example, if the insertion is successful, a message like "Insertion successful" should be shown.

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");
    }
 }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
NAHEEM OLANIYAN
  • 143
  • 2
  • 14
  • Note that the `visual-studio` tag is intended for questions about the Visual Studio application. Also, if you are using Entity Framework, please add a tag for it. – ProgrammingLlama Jan 25 '19 at 07:34
  • Hi i think you have to look at redirectToAction with parameter look at this https://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter – Sanpas Jan 25 '19 at 07:37
  • Side note: You should remove your `try`/`catch` block that adds no value (or if you think it adds value, at least make it `throw` rather than `throw ex;`) – Damien_The_Unbeliever Jan 25 '19 at 07:58
  • I find a solution by creating a new View inside the controller and redirecting to the page and the insertion is complete. But please is there a way to use "if statement" inside the controller to return the successful message? – NAHEEM OLANIYAN Jan 25 '19 at 09:42

3 Answers3

3

You are not getting any message because you are redirecting to Index page.

you may want to redirect to success page RedirectToAction("Success", "Shared").

you can use Error page for thrown exceptions which I believe you are already doing that by default.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

Try adding a parameter to the redirect (you will need to change the scope of latestProdId):

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

and then add an optional parameter to Index

public ActionResult Index(int latestProdId  = 0)

Test latestProdId for a non-zero value and then pass that back to the view to display.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
0

You might want to use TempData. It can be accessed in your view.

Controller:

TempData["status"] = "Success";

View:

{
    @TempData["status"];
}

You could do it like this:

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");
}