1

I'm just trying to put a SelectList into the ViewBag but I can't access it right after.

Here's my code:

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

If I use the debugger to step right after the ViewBag.originalLang assignation and use the expression evaluator, I get

Not found

However, if I go deeper into the ViewBag I can see

Expression evaluation

This is really weird and I don't get why I can't access it normally. Of course, I can't access it from the view either.

EDIT: Here's my View as erdi yılmaz requested:

@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") }
Winter
  • 3,894
  • 7
  • 24
  • 56
  • pls share your View – erdi yılmaz May 01 '17 at 14:21
  • 1
    You cannot use the same name for the property your binding to and the SelectList - refer [this answer](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557) (and it has nothing to do with the incorrect and bad practice answer you accepted) –  May 02 '17 at 10:15
  • @StephenMuecke Thank you for the explanation. The accepted answer might not be good pratice but it seems to work with me. Anyway, I've voting for dupplicate to link people to your answer. – Winter May 02 '17 at 12:14

3 Answers3

2

This is because the ViewBag is a dynamic type which means that it is resolved at runtime.

If you step through the debugger, you are looking at precompiled code and the ViewBag object has not yet been resolved by the DLR so you are unable to resolve the property.

What you are seeing inside the ViewBag when you "go deeper" are implementation details of how the data is collected for resolution.

JuanR
  • 7,405
  • 1
  • 19
  • 30
  • If it gets resolved at runtime, why doesn't it get resolved into the view ? I got lots of solutions and that's awesome but I really like to know what's going on. – Winter May 01 '17 at 14:32
  • @Winter: You should be able to see it if you debug the view. You may not be able to hover on the actual field but you should be able to "watch" the variable. – JuanR May 01 '17 at 14:54
2

Try this:

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

In your controller, you aren't returning a model to the view.. return View(/* empty here */);

So I don't know why you are using DropDownListFor. Instead use DropDownList

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • I followed the default pattern for CRUD controllers. I have a method to create from nothing and when it fails in the validation, it resend the view but with a Content this time. – Winter May 01 '17 at 14:34
  • Sorry, just a little confused.. if you don't have the line I provided above in both the `HttpGet` and `HttpPost` methods.. and only in the `HttpGet` method.. you should receive a run-time error on validation. You need to put the line I provided above in both `HttpGet` and `HttpPost` methods, before the modelstate is checked. – Grizzly May 01 '17 at 14:36
1

Try this for dropdown

@Html.DropDownListFor(model => model.originalLang, (IEnumerable<SelectListItem>)ViewBag.originalLang, new { htmlAttributes = new { @class = "form-control" } })
erdi yılmaz
  • 352
  • 1
  • 4
  • 15