-1

Hi people I am new to this website was having trouble with my controller in C# MVC3 and when I gave up on looking for answers since i spent like 2 weeks on it I decided to join here.

The problem is I want a very simple confirmation message when I create a item in my application. I tried a If statement but I can't get the context correct. Can you kind people please help me thank you. My code:

    //
    // POST: /News/Create

    [HttpPost]
    public ActionResult Create(BooksItem booksitem)
    {
        try
        {

            using (var db = new BooksForever2())
            {
                db.NewsItems.Add(booksitem);
                db.SaveChanges();
            }


            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

The create works fine I can add books and it saves but I want when it saves a message appears so it shows the user its has been saved. I have tried: Viewbag.Message("Saved") But this does not work. Any help will be truly appreciated

Thank You

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
user1137472
  • 345
  • 2
  • 5
  • 20

2 Answers2

5

Just add this in you controller

TempData["Message"] = "Saved";

then in your view:

@if(TempData["Message"] != null)
{
  <p>@TempData["Message"].ToString()</b> @* or whatever element you need to show*@
}

at your view level you can do anything with the message (maybe flash it with jQuery):

jquery: Flash messages

UPDATE: I replaced ViewBag with TempData because I noticed you are doing a redirect, in which case the ViewBag won't persist but TemData would

Community
  • 1
  • 1
Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
  • It is not a null item that i am checking, I have clearly stated my question that my add works fine and all i want is when some one enters a book a message is displayed saying it has been added – user1137472 Jan 08 '12 at 19:32
  • I just want something simple when a book is entered a message box or some kind of message says it has been saved that is all – user1137472 Jan 08 '12 at 19:38
  • 3
    Please remember that the answers provided here are usually entered by helpful people off the top of their head, without them having actually entered them into an development environment to validate the syntax. As such, you may need to tweak the syntax to get it working. Since the answer has pointed you in the right direction and even given you 95% of the code necessary, it is reasonable to expect that the questioner spend some minimum amount of effort trying to get it to work, rather than just dropping it into their application without understanding it. – Mike Mooney Jan 08 '12 at 19:44
0

Where do you want that confirmation message displayed? On the same edit form you are already on, or back on the index/list page?

Right now at the end of your method, you are redirecting to the Index action/page:

return RedirectToAction("Index");

The result of that is that the Index page will be loaded, and it will be completely unaware of where it came from other that something was saved.

Your two options, as I see it, are: 1) Stay on the current page, and display the message. You can add that message to the ViewBag like as has already been mentioned:

ViewBag.Message = "Saved"`

And then display it like this:

@if(ViewBag.Message != null)
{
   <p>@ViewBag.Message</p>
}

and then make sure you remove the RedirectToAction and just return the default View, otherwise will still bounce you to the Index page.

2) Or, you can redirect the user back to the Index page, passing the message to be displayed, and then have the Index page look for that message. So when you call RedirectToAction, include a query string parameter: ViewBag.Message

return RedirectToAction("Index", new { Message="Saved" });

Which will redirect you to ".../yourControllerName/Index?Message=Saved". Then you can add this to your Index action method:

if(!string.IsNullOrEmpty(QueryString["Message"]))
{
   ViewBag.Message = QueryString["Message"];
}

And include that same view code in your index view:

@if(ViewBag.Message != null)
{
   <p>@ViewBag.Message</p>
}
Mike Mooney
  • 11,729
  • 3
  • 36
  • 42
  • I tried the second option first and the if statement has been placed in my index view and it is showing as black like text nothing has been highlighted – user1137472 Jan 08 '12 at 19:43
  • So change the formatting of that text to display it however you want. – Mike Mooney Jan 08 '12 at 19:45
  • I mean it has not shown as code it does nothing in the view its like HTML text – user1137472 Jan 08 '12 at 19:46
  • If i put the @ symbol at the front of the if the QueryString shows an error and says "QueryString does not exist in the current context" – user1137472 Jan 08 '12 at 19:48
  • Take a few steps back and go turn through some ASP.NET MVC tutorials. You need a basic working knowledge of how .NET MVC web development works before you can expect stackoverflow will help you, and it seems like you are not there. – Mike Mooney Jan 08 '12 at 19:51
  • It seems you are giving code that does not work I tried your first option if you remove "RedirectToAction" the controller will not allow it as it needs it and your second code does not work either as it does not recognize the message if you knew what the error was you would have helped me other then just saying look at tutorials – user1137472 Jan 08 '12 at 19:54
  • 1
    My god, put at least a little bit of effort into this on your side. We can't drive to your house and write the code for you. Again, go learn some basic MVC, so that when I say "remove the RedirectToAction and just return the default View", you actually know that statement means. If you're not at that point, that's fine, but go learn the basics before yapping at everyone here trying to help you that "your code doesn't work". YOUR code doesn't work, and we're offering suggestions on how to fix it, but at the end of the day it is YOUR code. OWN IT. – Mike Mooney Jan 09 '12 at 01:48