I'm brand new to ASP.NET MVC
web development.
And I'm trying to build a website that basically has access to a database which can be filled by user.
Alright, all good, that part works fine. What I'm struggling with, is that whenever the user insert data into the database, I wanna show a message saying that his insertion has been done succesfully, or something like that. I wanna show that down in the form panel.
This is the code that I got so far:
This is the method being called when the user clicks the submit button:
[HttpPost]
public ActionResult Create(PersonModels person)
{
try
{
// TODO: Add insert logic here
//Adding to database and holding the response in the viewbag.
string strInsertion = ConnectionModels.insertPerson(person);
ViewBag.InsertionResult = strInsertion;
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
And this is my Index action:
public ActionResult Index()
{
return View();
}
And finally, this is what I'm trying to d in my index.cshtml:
<div>
<label>
@ViewBag.InsertionResult
</label>
</div>
I'm not gonna post it completely 'cause it'd be quite extensive. But that's beneath the div of the form panel.
I hope you can give me a hand.
Thanks in advance! :)
You can not pass a
ViewBag
value while redirecting to another action. If you are in the same controller you can useTempData
to pass values in a session otherwise you can pass the message as a parameter toRedirectionResult
like below:and then get it back like this:
This is a general way how to pass messages but I would recommend something like this:
Use a
BaseController
where all controllers inherits from this one:Here you can make a custom logic how to handle global message like error messages, notification messages, info messages etc.
For that you need to create a model like below:
I am keeping it simple here:
In
BaseController
you would have something like this:In this moment you just have to register new
GlobalMessage
inTempdata
like below:Then here is the final step how to show data in the view:
I personally use popup or modal window to do this: For example in bootstrapp you would write something like this:
Trigger the modal if there is a message:
This example was to show how you can make a system to show different messages. In short terms whatever you want!
ViewBag
andViewData
objects will returnnull
values after a redirect. Their contents only survive for the duration of the current request.There's another property,
TempData
, whose contents will endure the current and subsequent request, but only the subsequent request. However, this means that you can use it to pass data when you callRedirectToAction
.A good explanation is given here: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications/
An alternative approach would be for your
Index
view to take an optional view model containing the result of theHttpPost
that created the Person in your database. You could use that model to populate the contents of your label. Strongly typed models FTW!