我在MVS 3中创建了Web应用程序 但数据插入数据库
后无法显示警告消息控制器代码:
[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);
}
使用过的ViewData [“Message”] =“数据已插入”;这不显示消息:( 什么出错或我把它放在别的地方? 或者我可能有这个代码
<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>
但我不知道我在哪里声明了检查插入plz帮助的变量myvar
答案 0 :(得分:2)
在.chsthml页面上:
<script type="text/javascript">
$(document).ready(function () {
var msg = '@ViewBag.Message';
alert(msg);
});
</script>
在你的行动中:
ViewBag.Message = "1";
编辑:在脚本中应用条件检查:
<script type="text/javascript">
$(document).ready(function () {
var msg = '@ViewData["Message"]';
if(msg=='1')
alert("you are done with your thing");
});
</script>
在视图中:
ViewData["Message"] = "1";
ViewData["Message"]
会在这里产生同样的结果。
答案 1 :(得分:1)
尝试用户
TempData
而不是
ViewData