ViewBag内容未显示 [英] The ViewBag content isn't being visualized
问题描述
我是ASP.NET MVC
网站开发的新手.
而且我正在尝试建立一个基本上可以访问可由用户填充的数据库的网站.
I'm brand new to ASP.NET MVC
web development.
And I'm trying to build a website that basically has access to a database which can be filled by user.
好的,一切都很好,这部分工作正常.我正在努力的是,每当用户将数据插入数据库时,我都想显示一条消息,说明他的插入已成功完成,或类似的操作.我想在表单面板中显示出来.
Alright, all good, that part works fine. What I'm struggling with, is that whenever the user insert data into the database, I wanna show a message saying that his insertion has been done succesfully, or something like that. I wanna show that down in the form panel.
这是我到目前为止获得的代码:
This is the code that I got so far:
这是用户单击提交"按钮时要调用的方法:
This is the method being called when the user clicks the submit button:
[HttpPost]
public ActionResult Create(PersonModels person)
{
try
{
// TODO: Add insert logic here
//Adding to database and holding the response in the viewbag.
string strInsertion = ConnectionModels.insertPerson(person);
ViewBag.InsertionResult = strInsertion;
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
这是我的索引操作:
public ActionResult Index()
{
return View();
}
最后,这就是我要在index.cshtml中进行的操作:
And finally, this is what I'm trying to d in my index.cshtml:
<div>
<label>
@ViewBag.InsertionResult
</label>
</div>
我不会完全发布它,因为它会涉及面很广.但这在表单面板的div之下.
I'm not gonna post it completely 'cause it'd be quite extensive. But that's beneath the div of the form panel.
我希望你能帮上忙.
提前谢谢! :)
推荐答案
重定向到其他操作时,不能传递ViewBag
值.如果您在同一控制器中,则可以使用TempData
在会话中传递值,否则可以将消息作为参数传递给RedirectionResult
,如下所示:
You can not pass a ViewBag
value while redirecting to another action. If you are in the same controller you can use TempData
to pass values in a session otherwise you can pass the message as a parameter to RedirectionResult
like below:
return RedirectToAction("Index", new {message="Your Message"});
然后像这样将其恢复:
public ActionResult Index(string message)
{
ViewBag.ViewBag.InsertionResult = message;
return View();
}
这是一种传递消息的通用方法,但我建议使用类似的方法:
This is a general way how to pass messages but I would recommend something like this:
使用BaseController
,其中所有控制器均从该控制器继承:
Use a BaseController
where all controllers inherits from this one:
您可以在此处创建自定义逻辑,以处理错误消息,通知消息,信息消息等全局消息.
Here you can make a custom logic how to handle global message like error messages, notification messages, info messages etc.
为此,您需要创建一个如下所示的模型:
For that you need to create a model like below:
我在这里保持简单:
public class GlobalMessage
{
public string Message { get;set;}
public AlertType AlertType {get;set;}
}
public enum AlertType
{
Success, Info, Error, Danger//etc
}
在BaseController
中,您会遇到类似这样的情况:
In BaseController
you would have something like this:
public abstract class BaseController : Controller
{
protected GlobalMessage GlobalMessage;
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result is ViewResult)
{
if (GlobalMessage!= null)
{
filterContext.Controller.ViewBag.GlobalMessage = GlobalMessage;
}
else
{
GlobalErrorViewModel globalErrorModelView = TempData["GlobalMessage"] as GlobalMessage;
if (globalErrorModelView != null)
{
filterContext.Controller.ViewBag.GlobalErrorViewModel = globalErrorModelView;
}
}
}
base.OnActionExecuted(filterContext);
}
}
此刻,您只需要在Tempdata
中注册新的GlobalMessage
,如下所示:
In this moment you just have to register new GlobalMessage
in Tempdata
like below:
public PeopleController : BaseController
{
[HttpPost]
public ActionResult Create(PersonModels person)
{
try
{
// TODO: Add insert logic here
//Adding to database and holding the response in the viewbag.
string strInsertion = ConnectionModels.insertPerson(person);
TempData["GlobalMessage"] = new GlobalMessage{ AlertType = AlertType.Info, Message = "You have successfully added a new person" }
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
}
然后这是如何在视图中显示数据的最后一步:
Then here is the final step how to show data in the view:
我个人使用弹出窗口或模式窗口来执行此操作:例如,在bootstrapp中,您将编写如下内容:
I personally use popup or modal window to do this: For example in bootstrapp you would write something like this:
GlobalMessage globalMessage = ViewBag.GlobalMessage as GlobalMessage;
@if (globalMessage != null)
{
<!-- Modal -->
<div class="modal fade" id="globalMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content panel-@globalMessage .AlertType.ToString().ToLower() remove-border-radius">
<div class="modal-header panel-heading">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<p class="h-text-primary">@Html.Raw(globalMessage .Message)</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-@globalMessage .AlertType.ToString().ToLower() remove-border-radius" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
如果出现消息,则触发模态:
Trigger the modal if there is a message:
@if (globalMessage != null)
{
<script type="text/javascript">
$(document).ready(function () {
$('#globalMessage').modal('show');
});
</script>
}
此示例旨在说明如何使系统显示不同的消息.简而言之,随您便!
This example was to show how you can make a system to show different messages. In short terms whatever you want!
这篇关于ViewBag内容未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!