Shaiju T
Shaiju T

Reputation: 6609

alert messages on conditions using Json in asp.net mvc

i am trying to show alert message in the view using json, it works fine when conditions are false or true,

but i need to show muliple alert messages when return Json(false);

in action method:

if (hispoint < totalpoint)
{
 // alert("you need more points") ;                  

return Json(false, JsonRequestBehavior.AllowGet);      
}

else if (data1 < 2 )
{
// alert("you need more than 2") ;

 return Json(false, JsonRequestBehavior.AllowGet);
}

else if (data2 < 5 )
{
// alert("you need more than 5") ;

return Json(false, JsonRequestBehavior.AllowGet);
 }
 else
 {
 // redirect to another page
 return Json(true, JsonRequestBehavior.AllowGet);

 }

script in view:

 <script type="text/javascript">
$(document).ready(function () {
    $("a.GetAll").on("click", function (event) {
        event.preventDefault();
        $.get($(this).attr("href"), function (isEligible) {
            if (isEligible) {

                window.location.href = "@Url.Action("GetGift")";
            }


            else {

                alertify.alert('Sorry !, try again');

              }


        })
    });
});
</script>

my attempt:

string message = "you need more than 2";      
return Json(message, JsonRequestBehavior.AllowGet);

may i know how i can use else if in view and show alerts , i am newbie to json, any help would be great.

Upvotes: 0

Views: 2676

Answers (1)

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

Well, it is not the proper way to do, what you are trying to do. but if it just for learning, you can change your code like this to solve.

In action :

string msg;
if (hispoint < totalpoint)
{
    msg = "you need more points";
}
else if (data1 < 2 )
{
    msg = "you need more than 2";
}
else if (data2 < 5 )
{
    msg = "you need more than 5";
}
return Json(msg, JsonRequestBehavior.AllowGet);

Script :

<script type="text/javascript">
$(document).ready(function () {
$("a.GetAll").on("click", function (event) {
    event.preventDefault();
    $.get($(this).attr("href"), function (msg) {
        if (msg) {
            alertify.alert(msg);
        }
        else {
            window.location.href = "@Url.Action("GetGift")";
          }
        })
    });
}); 
</script>

Upvotes: 2

Related Questions