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

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

问题描述

直线前进的问题,似乎无法让我的viewBag值填写表格后,在视图中显示用户被定向到。

请advise..thanks

我的索引的ActionResult简单的回报模型数据。

 公众的ActionResult指数()
{
    无功源= _repository.GetByUserID(_applicationUser.ID);
    VAR模型=新RefModel
    {
        TEST1 = source.test1,
    };
    返回查看(模型);
}

我的获取编辑的ActionResult,只需使用相同的模型数据作为指标。

我邮报编辑的ActionResult,分配新值,如果任何模型并重定向到索引页,但索引页无法显示ViewBag值??

  [HttpPost]
公众的ActionResult编辑(RefModell模型)
{
    如果(ModelState.IsValid)
    {
        无功源= _repository.GetByUserID(_applicationUser.ID);
        如果(来源== NULL)返回视图(模型);        source.test1 = model.test1;
        _uow.SaveChanges();        @ ViewBag.Message =个人资料已成功更新;
        返回RedirectToAction(「指数」);
    }
    返回查看(模型);
}

在我的索引视图...

  @if(@ ViewBag.Message!= NULL)
{
    < D​​IV>
        <按钮式=按钮> @ ViewBag.Message< /按钮>
    < / DIV>
}


解决方案

ViewBag只活当前请求。在你的情况要重定向,所以你可能已经存储在ViewBag一切都将一起死机智当前请求。使用ViewBag,只有当你渲染视图,如果你不打算重定向。

使用的TempData 而不是:

 的TempData [消息] =个人资料更新成功;
返回RedirectToAction(「指数」);

,然后在您的视图:

  @if(TempData的[消息]!= NULL)
{
    < D​​IV>
        <按钮式=按钮> @TempData [消息] LT; /按钮>
    < / DIV>
}

在幕后,TempData的将使用会话,但一旦你从中读取它就会自动退出的纪录。所以基本上用的短版活一重定向的持久化存储。

另外,您可以把它作为查询字符串参数,如果你不希望依靠会话(这可能是我会做什么)。

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屋!

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