【问题标题】:ViewBag property set but not accessible [duplicate]ViewBag 属性集但不可访问[重复]
【发布时间】:2017-09-28 22:36:23
【问题描述】:

我只是想将一个 SelectList 放入 ViewBag,但之后我无法访问它。

这是我的代码:

// GET: Content/CreateSerie
public ActionResult CreateSerie()
{
    ViewBag.originalLang = new SelectList(db.Lang, "originalLang", "originalLang");
    return View();
}

如果我使用调试器在 ViewBag.originalLang 分配之后执行步骤并使用表达式评估器,我会得到

但是,如果我深入查看 ViewBag,我可以看到

这真的很奇怪,我不明白为什么我不能正常访问它。当然,我也无法从视图中访问它。

编辑:这是 erdi yılmaz 要求的我的观点:

@model e08projh17.Models.Content

@{
    ViewBag.Title = "Créer une série";
    @Styles.Render("~/Content/css")
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
}

<h2>CreateSerie</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Content</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <!-- Lots of stuff ... -->


        <div class="form-group">
            @Html.LabelFor(model => model.originalLang, "Langue originale", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.originalLang, (SelectList)ViewBag.originalLang, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.originalLang, "", new { @class = "text-danger" })
            </div>
        </div>


        <!-- Lots of stuff ... -->

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>@Html.ActionLink("Back to List", "Index")</div>

@section Scripts { @Scripts.Render("~/bundles/jqueryval") }

【问题讨论】:

  • 请分享您的看法
  • 您不能对绑定到的属性和 SelectList 使用相同的名称 - 请参阅 this answer(这与您接受的错误和不良做法答案无关)
  • @StephenMuecke 谢谢你的解释。接受的答案可能不是很好的做法,但它似乎对我有用。无论如何,我已经投票支持重复以将人们与您的答案联系起来。

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

试试这个:

@Html.DropDownList("originalLang", null, htmlAttributes: new { @class = "form-control" } })

在您的控制器中,您不会将模型返回到视图中。return View(/* empty here */);

所以我不知道你为什么使用DropDownListFor。而是使用DropDownList

【讨论】:

  • 我遵循了 CRUD 控制器的默认模式。我有一种从无到有创建的方法,当它在验证中失败时,它会重新发送视图,但这次有一个内容。
  • 对不起,有点困惑..如果您在HttpGetHttpPost 方法中都没有我上面提供的行...并且仅在HttpGet 方法中..您应该在验证时收到运行时错误。在检查模型状态之前,您需要将我上面提供的行放在 HttpGetHttpPost 方法中。
【解决方案2】:

这是因为 ViewBag 是动态类型,这意味着它是在运行时解析的。

如果您单步调试调试器,您正在查看预编译代码,并且 DLR 尚未解析 ViewBag 对象,因此您无法解析该属性。

当您“深入”时,您在 ViewBag 中看到的是如何收集数据以进行解析的实施细节。

【讨论】:

  • 如果它在运行时被解析,为什么它不被解析到视图中?我有很多解决方案,这很棒,但我真的很想知道发生了什么。
  • @Winter:如果您调试视图,您应该能够看到它。您可能无法将鼠标悬停在实际字段上,但您应该能够“观察”变量。
【解决方案3】:

试试这个下拉菜单

@Html.DropDownListFor(model => model.originalLang, (IEnumerable<SelectListItem>)ViewBag.originalLang, new { htmlAttributes = new { @class = "form-control" } })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2022-01-22
    相关资源
    最近更新 更多