2013-04-02 51 views
0

我正在使用MVC,並且正在檢查後端(控制器)上的情況。 我想在一個Jquery模式框中顯示消息,它將從後端(控制器)觸發。如何在MVC中顯示來自控制器的jquery消息

任何人都可以告訴我如何做到這一點。

我試圖使用下面的代碼,但這是給我一些消息:無效的參數。

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

你能告訴我怎麼用上面的語句在MVC

感謝

+0

嘗試製作使用Ajax。 – ssilas777

+0

我不想使用它。 –

+0

在TempData中傳遞一些標誌值更多信息http://www.squaredroot.com/2007/12/20/mvc-viewdata-vs-tempdata/ – Amit

回答

0

您可以使用Ajax作爲ssilas777這樣做已經註釋掉。

使用以下$ .ajax代碼。

$(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){ 
     } 
    }); 
}); 

這裏是控制器代碼。在腳本

public ActionResult Index(){ 
    //do some stuff 
    string Message="your message here !"; 
    return Json(Message,JsonRequestBehaviour.AllowGet); 
} 
+0

謝謝Karthik我已經使用了這個選項,但我正在尋找一種方法,可以顯示控制器的消息。 –

+0

@Evostract - 你檢查** [這裏也是](http://stackoverflow.com/questions/15664193/what-replaced-clientscriptmanager-in-mvc)**? – 2013-04-02 12:08:32

0

在控制器,設置ViewBag旗地爲它想:

ViewBag.Login = "No"; 

然後在查看檢查Document.Ready是否設置,並在那裏給出警報消息。

<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(); 
} 

0

使用下面的代碼鑑於

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

控制器:

public ActionResult Index(){ 

    //do some stuff 
    string msg="your message"; 
    return Json(msg,JsonRequestBehaviour.AllowGet); 
} 
相關問題