Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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# ViewBag属性集,但不可访问_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# ViewBag属性集,但不可访问

C# ViewBag属性集,但不可访问,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我只是想把一个SelectList放到ViewBag中,但是我不能马上访问它 这是我的密码: // GET: Content/CreateSerie public ActionResult CreateSerie() { ViewBag.originalLang = new SelectList(db.Lang, "originalLang", "originalLang"); return View(); } 如果我使用调试器在ViewBag.originalLang赋值之后

我只是想把一个SelectList放到ViewBag中,但是我不能马上访问它

这是我的密码:

// GET: Content/CreateSerie
public ActionResult CreateSerie()
{
    ViewBag.originalLang = new SelectList(db.Lang, "originalLang", "originalLang");
    return View();
}
如果我使用调试器在
ViewBag.originalLang
赋值之后直接执行,并使用表达式计算器,我会得到

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

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

编辑:根据埃尔迪·伊尔马兹的要求,以下是我的观点:

@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") }
@model e08projh17.Models.Content
@{
ViewBag.Title=“Créer une série”;
@style.Render(“~/Content/css”)
@style.Render(“~/Content/themes/base/css”)
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/jqueryval”)
@Scripts.Render(“~/bundles/jqueryui”)
}
CreateSerie
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
所容纳之物

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.originalLang,“Langue originale”,htmlAttributes:new{@class=“control label col-md-2”}) @DropDownListFor(model=>model.originalLang,(SelectList)ViewBag.originalLang,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.originalLang,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{@Scripts.Render(“~/bundles/jqueryval”)}
这是因为ViewBag是一种动态类型,这意味着它是在运行时解析的

如果单步执行调试器,您将看到预编译的代码,并且DLR尚未解析ViewBag对象,因此无法解析该属性


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

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

如果单步执行调试器,您将看到预编译的代码,并且DLR尚未解析ViewBag对象,因此无法解析该属性

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

尝试以下方法:

@Html.DropDownList("originalLang", null, htmlAttributes: new { @class = "form-control" } })
在控制器中,您没有将模型返回到视图<代码>返回视图(/*此处为空*/)

所以我不知道你为什么要使用
DropDownListFor
。请改用
DropDownList

尝试以下方法:

@Html.DropDownList("originalLang", null, htmlAttributes: new { @class = "form-control" } })
在控制器中,您没有将模型返回到视图<代码>返回视图(/*此处为空*/)

所以我不知道你为什么要使用
DropDownListFor
。请改用
DropDownList

尝试使用此下拉列表

@Html.DropDownListFor(model => model.originalLang, (IEnumerable<SelectListItem>)ViewBag.originalLang, new { htmlAttributes = new { @class = "form-control" } })
@Html.DropDownListFor(model=>model.originalLang,(IEnumerable)ViewBag.originalLang,new{htmlAttributes=new{@class=“form control”})
试试这个下拉列表

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

请分享您的观点。您绑定到的属性和SelectList不能使用相同的名称-请参考(这与您接受的错误和错误的实践答案无关)@StephenMuecke感谢您的解释。被接受的答案可能不是一个好的实践,但它似乎与我的工作。无论如何,我投票支持dupplicate将人们链接到你的答案。请分享你的观点。你绑定到的属性和SelectList不能使用相同的名称-请参考(这与你接受的错误和错误的实践答案无关)@StephenMuecke感谢你的解释。被接受的答案可能不是一个好的实践,但它似乎与我的工作。无论如何,我投票支持dupplicate将人们链接到您的答案。如果它在运行时得到解决,为什么不在视图中得到解决?我有很多解决方案,这太棒了,但我真的很想知道发生了什么。@Winter:如果调试视图,您应该可以看到它。您可能无法将鼠标悬停在实际字段上,但应该能够“监视”变量。如果在运行时解析该变量,为什么不将其解析到视图中?我有很多解决方案,这太棒了,但我真的很想知道发生了什么。@Winter:如果调试视图,您应该可以看到它。您可能无法在实际字段上悬停,但应该能够“监视”变量。我有一个方法可以从无到有地创建,当它在验证中失败时,它会重新发送视图,但这次会包含一个内容。对不起,只是有点困惑。。如果在
HttpGet
HttpPost
方法中都没有我上面提供的行。。并且仅在
HttpGet
方法中。。您应该在验证时收到运行时错误。在检查modelstate之前,您需要在
HttpGet
HttpPost
方法中放置我上面提供的行。我遵循CRUD控制器的默认模式。我有一个方法可以从无到有地创建,当它在验证中失败时,它会重新发送视图,但这次会包含一个内容。对不起,只是有点困惑。。如果在
HttpGet
HttpPost
方法中都没有我上面提供的行。。并且仅在
HttpGet
方法中。。您应该在验证时收到运行时错误。在检查modelstate之前,需要将上面提供的行放入
HttpGet
HttpPost
方法中。