0

I am using MVC and I am checking the condition on the backend(controller). I want to show message in a Jquery modal box which will be fired from the back end(controller).

Can anyone state me how to do this.

I have tried to use the following code but that is giving me some message: Invalid Argument.

string scriptstring = "$(function(){initializedialog();showDialog(\"" + "Please answer to all the Question." + "\");});";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "test", scriptstring , true);

Can you tell me how to use the above statement in MVC

Thanks

Aman Rohilla
  • 624
  • 3
  • 11
  • 25

3 Answers3

0

In Controller, Set ViewBag Flag for it like :

 ViewBag.Login = "No";

Then in View Check on Document.Ready whether it is set or not and give your alert message there.

 <script>
$(document).ready(function () {
    varifyForLogin();
});
function varifyForLogin() {
    if ('@ViewBag.Login' == 'No') {
        //Give your alert message here
        alert('Please answer to all the Question.');
    }
    document.getElementById('UserName').focus();
}

ravisolanki07
  • 637
  • 3
  • 13
0

You can do so with ajax as ssilas777 has commented out.

use the following $.ajax code.

$(function(){
    $.ajax({
        url:'@Url.Action("Index","Home")',
        type: 'GET',
        dataType:'json',
        success:function(data){
            //Here you will get the data.
            //Display this in your Modal popup
        },
        error: function(data){
        }
    });
});

Here is the controllers code .

public ActionResult Index(){
    //do some stuff
    string Message="your message here !";
    return Json(Message,JsonRequestBehaviour.AllowGet);
}
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
  • Thanks Karthik I have already used this option but I was looking for for a method where I can show the message from the controller. – Aman Rohilla Apr 02 '13 at 11:25
  • @Evostract - Did u check **[here also](http://stackoverflow.com/questions/15664193/what-replaced-clientscriptmanager-in-mvc)** ? –  Apr 02 '13 at 12:08
0

Use following code in script in view

<script>
$.post("Home/Index", function(result) {
<br/>
    &nbsp;&nbsp;&nbsp;&nbsp;//result will be your message
<br/>
});
</script>
<br/>

Controller:

public ActionResult Index(){

    //do some stuff
    string msg="your message";
    return Json(msg,JsonRequestBehaviour.AllowGet);
}
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
Manpreet
  • 29
  • 2