Reputation: 16219
I have create web app in MVS 3 but failed to display alert message after data insert into database
Controller code:
[HttpPost]
public ActionResult QuestionBank(QuestionBank questionbank)
{
if (ModelState.IsValid)
{
dbEntities.QuestionBanks.AddObject(questionbank);
dbEntities.SaveChanges();
//questionbank.SendEnquiryEmail(questionbank);
ViewData["Message"] = "Data inserted";
return RedirectToAction("QuestionBank");
}
return View(questionbank);
}
Used ViewData["Message"] = "Data inserted"; which is not displayed message :( whats going wrong or i placed it somewhere else? OR ELSE I MAY HAVE THIS CODE
<script type="text/javascript">
//i'm using jquery ready event which will call the javascript chunk after the page has completed loading
$(document).ready(function () {
//assuming that your variable name from the code behind is bInsertSuccess
var bSuccess = "<%= myvar %>";
if (bSuccess) {
alert("Successfully Inserted");
}
});
</script>
but i dont know where i declare that variable myvar
which checks insertion plz help
Upvotes: 1
Views: 8749
Reputation:
On your .chsthml page:
<script type="text/javascript">
$(document).ready(function () {
var msg = '@ViewBag.Message';
alert(msg);
});
</script>
in your action:
ViewBag.Message = "1";
Edit: Apply conditional check in script:
<script type="text/javascript">
$(document).ready(function () {
var msg = '@ViewData["Message"]';
if(msg=='1')
alert("you are done with your thing");
});
</script>
In view:
ViewData["Message"] = "1";
ViewData["Message"]
would result in same thing here.
Upvotes: 2