Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不是';t viewbag值传递回视图?_C#_Html_Asp.net Mvc 4 - Fatal编程技术网

C# 为什么不是';t viewbag值传递回视图?

C# 为什么不是';t viewbag值传递回视图?,c#,html,asp.net-mvc-4,C#,Html,Asp.net Mvc 4,直截了当的问题是,似乎无法让我的viewBag值显示在用户填写表单后直接指向的视图中 请建议……谢谢 我的索引ActionResult简单返回模型数据 public ActionResult Index() { var source = _repository.GetByUserID(_applicationUser.ID); var model = new RefModel { test1 = source.test1, }; retur

直截了当的问题是,似乎无法让我的viewBag值显示在用户填写表单后直接指向的视图中

请建议……谢谢

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

public ActionResult Index()
{
    var source = _repository.GetByUserID(_applicationUser.ID);
    var model = new RefModel
    {
        test1 = source.test1,
    };
    return View(model);
}
我的Get Edit“ActionResult只使用与索引相同的模型数据

我的文章“编辑”ActionResult,为模型分配新值(如果有),并重定向到索引页,但索引页不显示ViewBag值

[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);
}
在我的索引视图中

@if(@ViewBag.Message != null)
{
    <div>
        <button type="button">@ViewBag.Message</button>
    </div>
}
@if(@ViewBag.Message!=null)
{
@查看包。留言
}

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

改用
TempData

TempData["Message"] = "Profile Updated Successfully";
return RedirectToAction("Index");
在你看来:

@if (TempData["Message"] != null)
{
    <div>
        <button type="button">@TempData["Message"]</button>
    </div>
}
@if(TempData[“Message”!=null)
{
@TempData[“消息”]
}
在幕后,TempData将使用Session,但一旦您从中读取记录,它将自动收回该记录。因此,它基本上是用于短期的重定向持久性存储


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

重定向操作会导致HTTP 302响应,这会使客户端再次调用服务器并请求新页面

您应该返回视图而不是重定向。

重定向操作()指示浏览器发出新请求。
因此,您的服务器将再次被调用,但这将是一个新请求,其中包含一个空白的viewbag和所有
您可以通过调用index方法进行某种内部重定向,这样viewbag仍将保留其数据。

编辑:您还必须修改索引方法,否则视图(模型)行将尝试呈现编辑。
下面的完整代码

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


[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 Index();      
    }
    return View(model);
}

你也可以这样试试

控制器

public ActionResult Test()
    {
ViewBag.controllerValue= "testvalue";
 ..................
    }
查看- 定义页面顶部
@{string testvalue=(string)ViewBag.controllerValue;}

$(function () {
       var val= '@testvalue';
});


嗯,实际上,在成功发布后重定向是最好的做法。只有当用户需要修复并重新提交一些错误时才返回视图。用户以直截了当的方式提问,我给出了一个直截了当的答案,说明为什么没有显示值。我不知道,将其存储在会话中的缺点是什么我的观点是,如果你想扩展,它会给你带来麻烦。我倾向于直接返回一个视图,而不是将它存储在会话中(你应该对此保持警惕)Darin对解决方案的看法是正确的,但问题不是他的架构是否正确,而是为什么没有显示该值。只是为了进一步扭转这个问题……如果我返回的视图是一个选项卡式视图,而我重定向的唯一方法是通过..return RedirectPermanent(~/RefTarget/Index/#users))怎么办..#用户是三个选项卡中的一个…那么如何执行我的viewBag消息操作?您正在再次执行重定向。我在viewmodel或viewBag中设置所选选项卡,并在视图中检查此值,以便在渲染时选择正确的选项卡。很好!对于无法返回视图的用户,因为他们必须创建一个再次建模这是一个很好的解决方案!这样你就不必在两个地方创建模型,从而减少代码重复。太棒了!