我的ViewBag无法正常工作的任何原因? [英] Any reason why my ViewBag is not working?

查看:67
本文介绍了我的ViewBag无法正常工作的任何原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在controller中关注了ActionResult,如果成功,您会看到我在ViewBag中设置了一条消息.然后在View上,如果不为空,则应输出该消息.但是,我无法显示该消息,也没有发现问题所在.

I have following ActionResult in a controller and you can see that I set a message in the ViewBag if it's successful. Then on the View it should output that message if it's not empty. However, I can't get the message to display and I'm not seeing what the problem is.

[HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
                {
                    Name = collection["RoleName"]
                });
                context.SaveChanges();

                ViewBag.ResultMessage = "Role created successfully.";
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View();
            }            
        }

这是我的Index.cshtml

This is my Index.cshtml

@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
@{
    ViewBag.Title = "Index";
}

<h2>Roles Listing </h2>

@ViewBag.ResultMessage

@Html.ActionLink("Create New Role", "Create") | @Html.ActionLink("Manage User Role", "ManageUserRoles")

<div>
    <table class="table table-bordered table-condensed table-striped table-hover ">
        <thead>
            <tr>
                <th>Role</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var role in Model)
            {
                <tr>
                    <td><strong>@role.Name</strong></td>
                    <td>
                        <span onclick="return confirm('Are you sure you want to delete @role.Name?')"><a href="/Roles/Delete?RoleName=@role.Name" class="delLink" style="color:red;">Delete</a></span> |
                        @Html.ActionLink("Edit", "Edit", new { roleName = @role.Name })
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

推荐答案

ViewBag可帮助您在从controller移到view时维护数据.寿命短意味着重定向发生时值变为空.这是因为他们的目标是提供一种在controllersviews之间进行通信的方式.这是服务器调用中的一种通信机制.

ViewBag helps to maintain data when you move from controller to view. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.

由于使用的是RedirectToAction,当ViewBag到达view时将变为null.

Since you are using RedirectToAction, the ViewBag becomes null when it reaches the view.

您可以为此使用TempData:

TempData["ResultMessage"] = "Role created successfully.";

它使用Session作为存储,但是在第二次响应后不会出现. 当您从一个controller移至另一个controller或从一个动作移至另一动作时,TempData有助于维护数据.换句话说,当您重定向时,Tempdata帮助维护这些重定向之间的数据.它在内部使用会话变量. TempData在当前请求和后续请求中使用仅表示您确定下一个请求将重定向到下一个视图时使用.

It uses Session as storage, but it will not be around after the second response. TempData helps to maintain data when you move from one controller to other controller or from one action to other action. In other words, when you redirect, Tempdata helps to maintain data between those redirects. It internally uses session variables. TempData use during the current and subsequent request only means it is used when you are sure that next request will be redirecting to next view.

有关此的更多理解,请参见此链接

For more understanding on this refer this link

这篇关于我的ViewBag无法正常工作的任何原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆