How to trigger a popup through an else statement in C#
Hi i'm using MVC3 and i'm in the C# environment. I would like to have a popup show when an else statement is satisfied. the code looks like this:
if (validate.Count < 1) {
db.TutorStudents.Add(tutorStudent);
db.SaveChanges();
return RedirectToAction("Index");
} else {
// need some form of popup message here that does not redirect the user
}
I've been searching and 开发者_运维百科I found JQuery solutions, but they work on a click result right... rather, I want the popup to work when the else statement is reached. Any solution is welcome.
Thank you in advance
You can't show popup on the client side from the server directly.
But you can put some marker value to the ViewData
or ViewBag
and pass this value to the script in your view:
in the action
if (validate.Count < 1)
{
db.TutorStudents.Add(tutorStudent);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
ViewBag.ShowMessage = true;
//need some form of popup message here that does not redirect the user
}
in the view
@if (ViewBag.ShowMessage)
{
<script type="text/javascript">
$(document).ready(function () {
$(".message-box").show();
});
</script>
<div class="message-box">Some Message here</div>
}
try:
if (validate.Count < 1){
db.TutorStudents.Add(tutorStudent);
db.SaveChanges();
return RedirectToAction("Index");
} else {
ViewBag.PopUp = true;
return View(); // or any logic to show the last-view to user again
}
and in view:
@if(ViewBag.PopUp == true){
<script type="text/javascript">
alert("Your message here...");
</script>
}
UPDATE: You can't show a popup to user via C# in Web-Apps. Because C# runs at server, and you have to create a client-side code to run on end-users browser. So, you must create a logic via C# to control App and generate HTML (and/or JavaScript, jQuery, CSS) to achieve your purpose.
精彩评论