为什么 viewbag 值不传递回视图? [英] Why isn't viewbag value passing back to the view?

查看:24
本文介绍了为什么 viewbag 值不传递回视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直截了当的问题,似乎无法让我的 viewBag 值显示在用户完成表单后被定向到的视图中.

请指教..谢谢

My Index ActionResult 简单返回模型数据..

public ActionResult Index(){var source = _repository.GetByUserID(_applicationUser.ID);var 模型 = 新的 RefModel{test1 = source.test1,};返回视图(模型);}

My Get Edit" ActionResult ,简单地使用与索引相同的模型数据.

我的帖子编辑"ActionResult,将新值分配给模型并重定向到索引页面,但索引页面不显示ViewBag值??

[HttpPost]公共 ActionResult 编辑(RefModell 模型){如果(模型状态.IsValid){var source = _repository.GetByUserID(_applicationUser.ID);if (source == null) return View(model);source.test1 = 模型.test1;_uow.SaveChanges();@ViewBag.Message = "个人资料更新成功";return RedirectToAction("索引");}返回视图(模型);}

在我的索引视图中...

@if(@ViewBag.Message != null){<div><button type="button">@ViewBag.Message</button>

}

解决方案

ViewBag 只对当前请求有效.在您的情况下,您正在重定向,因此您可能存储在 ViewBag 中的所有内容都将随着当前请求而消失.仅当您呈现视图时才使用 ViewBag,而不是在您打算重定向时使用.

使用 TempData 代替:

TempData["Message"] = "配置文件更新成功";return RedirectToAction("索引");

然后在您看来:

@if (TempData["Message"] != null){<div><button type="button">@TempData["Message"]</button>

}

在幕后,TempData 将使用 Session,但一旦您从中读取记录,它就会自动驱逐记录.所以它基本上用于短期单重定向持久化存储.

或者,如果您不想依赖会话,也可以将其作为查询字符串参数传递(这可能是我会做的).

straight forward question , can't seem to get my viewBag value to display in a view that the user is directed to after completing a form.

Please advise..thanks

My Index ActionResult simple returns model data..

public ActionResult Index()
{
    var source = _repository.GetByUserID(_applicationUser.ID);
    var model = new RefModel
    {
        test1 = source.test1,
    };
    return View(model);
}

My Get Edit" ActionResult , simply uses the same model data as Index.

My Post "Edit" ActionResult, assigns the new values if any to the model and redirects to the Index page, but Index page does not display ViewBag value ??

[HttpPost]
public ActionResult Edit(RefModell model)
{
    if (ModelState.IsValid)
    {
        var source = _repository.GetByUserID(_applicationUser.ID);
        if (source == null) return View(model);

        source.test1 = model.test1;
        _uow.SaveChanges();

        @ViewBag.Message = "Profile Updated Successfully";
        return RedirectToAction("Index");      
    }
    return View(model);
}

And in my Index view...

@if(@ViewBag.Message != null)
{
    <div>
        <button type="button">@ViewBag.Message</button>
    </div>
}

解决方案

ViewBag only lives for the current request. In your case you are redirecting, so everything you might have stored in the ViewBag will die along wit the current request. Use ViewBag, only if you render a view, not if you intend to redirect.

Use TempData instead:

TempData["Message"] = "Profile Updated Successfully";
return RedirectToAction("Index");

and then in your view:

@if (TempData["Message"] != null)
{
    <div>
        <button type="button">@TempData["Message"]</button>
    </div>
}

Behind the scenes, TempData will use Session but it will automatically evict the record once you read from it. So it's basically used for short-living, one-redirect persistence storage.

Alternatively you could pass it as query string parameter if you don't want to rely on sessions (which is probably what I would do).

这篇关于为什么 viewbag 值不传递回视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆