Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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
Asp.net mvc mvc将数据从控制器传递到其他控制器的视图_Asp.net Mvc_Viewbag_Viewdata - Fatal编程技术网

Asp.net mvc mvc将数据从控制器传递到其他控制器的视图

Asp.net mvc mvc将数据从控制器传递到其他控制器的视图,asp.net-mvc,viewbag,viewdata,Asp.net Mvc,Viewbag,Viewdata,我有一个AccountController,其中有一个操作表单,在这个操作中,我使用RedirectToActionIndex(默认值)重定向到另一个视图。现在我想在默认的索引页上显示一条消息。我尝试使用ViewBag或ViewData传递值,但它仍然为空,并且无法在索引上使用其值 会计控制员 public ActionResult YNHHConsentForm(YNHHFormVM model) { if (result == 0 || isSuccess ==

我有一个AccountController,其中有一个操作表单,在这个操作中,我使用RedirectToActionIndex(默认值)重定向到另一个视图。现在我想在默认的索引页上显示一条消息。我尝试使用ViewBag或ViewData传递值,但它仍然为空,并且无法在索引上使用其值

会计控制员

   public ActionResult YNHHConsentForm(YNHHFormVM model)
    {
        if (result == 0 || isSuccess == false)
        {
            model.FormSubmissionMessage = "Something went wrong please try again";
            return View(model);
        }
        else
        {
            SessionItems.IsAuthorizationFormFilled = true;
            //ViewBag.FormSubmissionMessage="Form submitted successfully";
            ViewData["FormSubmissionMessage"] = "Form submitted successfully";
            return RedirectToAction("Index", "Default");
        }
    }
索引默认值

   @if (ViewData["FormSubmissionMessage"] !=null)
            {
                <div class="alert alert-success">
                    ViewData["FormSubmissionMessage"].ToString()
                </div>
            }
我是第一次使用ViewBag和ViewData,因此无法找出哪里做错了。

您需要使用TempData。在形式方法中

然后在Index方法中访问它,并将其添加到ViewBag中,这样您就可以在视图中访问它

ViewBag.MyMessage = TempData["FormSubmissionMessage"];
在我看来

<div class="alert alert-success">@ViewBag.MyMessage</div>
有关ViewData ViewBag和TempData之间差异的说明,请参阅

旁注:TempData只持续一次重定向,因此如果用户刷新浏览器,则不会再次生成消息。如果这是一个关键问题,您可以使用.Keep或.Peek来解决此问题。有关更多详细信息,请参阅您需要使用TempData。在形式方法中

然后在Index方法中访问它,并将其添加到ViewBag中,这样您就可以在视图中访问它

ViewBag.MyMessage = TempData["FormSubmissionMessage"];
在我看来

<div class="alert alert-success">@ViewBag.MyMessage</div>
有关ViewData ViewBag和TempData之间差异的说明,请参阅


旁注:TempData只持续一次重定向,因此如果用户刷新浏览器,则不会再次生成消息。您可以使用.Keep或.Peek来解决此问题,如果其关键是参考更多详细信息

为了补充@StephenMueke的答案,我通常会为您使用的场景创建一对组件来显示系统确认。它包括:

一个静态类,包含作为字符串的状态/警报类型。 在布局上呈现的局部视图,以便消息位于一致的位置。 静态类如下所示:

public class Alert
{
    public static string Error = "alert-error alert-danger";
    public static string Info = "alert-info";
    public static string Warning = "alert-warning";
    public static string Success = "alert-success";

    public static string[] All = new string[]{ Error, Info, Warning, Success };
}
本质上,这些是引导警报类,但它们足够通用,可以在没有引导的情况下工作。Alert.Error值包含两个类,使其与引导程序的版本2和版本3兼容

部分视图将检查TempData中的警报值,如果找到,将生成引导警报:

@foreach (string key in Alert.All)
{
    if (TempData.ContainsKey(key))
    {
        <div class="alert @key">
            <button type="button" class="close" data-dismiss="alert"><i class="fa fa-fw fa-times"></i></button>
            @Html.Raw(TempData[key])
        </div>
    }
}
将改为:

谢谢大家!!表单提交成功


为了补充@StephenMueke的答案,我通常会为您使用显示系统确认的场景创建一对组件。它包括:

一个静态类,包含作为字符串的状态/警报类型。 在布局上呈现的局部视图,以便消息位于一致的位置。 静态类如下所示:

public class Alert
{
    public static string Error = "alert-error alert-danger";
    public static string Info = "alert-info";
    public static string Warning = "alert-warning";
    public static string Success = "alert-success";

    public static string[] All = new string[]{ Error, Info, Warning, Success };
}
本质上,这些是引导警报类,但它们足够通用,可以在没有引导的情况下工作。Alert.Error值包含两个类,使其与引导程序的版本2和版本3兼容

部分视图将检查TempData中的警报值,如果找到,将生成引导警报:

@foreach (string key in Alert.All)
{
    if (TempData.ContainsKey(key))
    {
        <div class="alert @key">
            <button type="button" class="close" data-dismiss="alert"><i class="fa fa-fw fa-times"></i></button>
            @Html.Raw(TempData[key])
        </div>
    }
}
将改为:

谢谢大家!!表单提交成功