为什么我的视图不显示ViewBag的值?

Ger*_*imo 1 c# asp.net asp.net-mvc razor asp.net-mvc-4

我有一个带有帖子和标签的小博客应用程序。这是我的Post模型:

namespace HelloWorld.Models
{
    public class Post
    {
        [Required]
        [DataType(DataType.Text)]
        public string Title { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime PostDate { get; set; }

        public List<Tag> Tags { get; set; }

        [Required]
        public int PostId { get; set; }
    }

    public class CreatePostView
    {
        [Required]
        [DataType(DataType.Text)]
        public string Title { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Display(Name = "Tags")]
        [Required(ErrorMessage = "Please select a tag")]
        public string SelectedTag { get; set; }
        public SelectList TagList { get; set; }

        [Required]
        public int PostId { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tag的模型由字符串TagNameint TagId列表帖子组成

创建新帖子时,我使用CreatePostView,而我的视图是:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="create-post-form">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">

            <strong>Title</strong>

            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">

            <strong>Description</strong>

            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.DropDownListFor(m => m.SelectedTag, Model.TagList, "Add tag")
        @Html.ValidationMessageFor(m => m.SelectedTag)

        <div class="post-create-button">
            <input type="submit" value="Create">
        </div>

        <div class="back-to-list-button">
            @Html.ActionLink("Back", "Index")
        </div>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

现在,我想显示我选择的标签。我将选定标签的值放在ViewBag中,但不显示。也许这很愚蠢,但我不知道如何解决。我的PostsController创建动作:

// POST: Posts/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreatePostView post)
        {
            Post currPost = new Post {
                                       Title = post.Title, 
                                       Description = post.Description, 
                                       PostDate = DateTime.Now, 
                                       Tags = null };

            ViewBag.Tag = post.SelectedTag.ToString();
            ViewBag.Trash = "texttexttexttexttext"; // It's strange, but it not displayed.

            if (ModelState.IsValid)
            {
                //var tags = db.Tags.Where(s => s.TagName.Equals(post.SelectedTag)).ToList();     
                //currPost.Tags = tags;

                db.Posts.Add(currPost);
                db.SaveChanges();
                return RedirectToAction("Index", "Posts");
            }

            return View(currPost);
        }
Run Code Online (Sandbox Code Playgroud)

我对所有帖子的看法(使用Post模型)

@foreach (var item in Model)
    {
        <article class="post">
            <h3>@Html.DisplayFor(modelItem => item.Title)</h3>
            <p>@Html.DisplayFor(modelItem => item.Description)</p>

            <!--None of them is not shown-->
            <p><strong>Tag: @ViewBag.Tag</strong></p>
            <p><strong>Trash: @ViewBag.Trash</strong></p>
        </article>
    }
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 5

ViewBag在返回视图时使用,而不是在重定向到另一个操作时使用。基本上,它不会在单独的请求中持续存在。尝试TempData改用:

TempData["Tag"] = post.SelectedTag.ToString();
Run Code Online (Sandbox Code Playgroud)

并在视图中:

<p><strong>Tag: @TempData["Tag"]</strong></p>
Run Code Online (Sandbox Code Playgroud)