Search code examples
asp.net-mvcasp.net-mvc-4viewbag

ViewBag does not work


I'm trying to create an MVC site and I have some trubble with the ViewBag.

It's like it can't contain elements.

I'm using MVC 4.

This is my function in the controller:

public ActionResult Create()
{
    ViewBag.DiscID = new SelectList(entities.Disc, "ID", "ID");
    ViewBag.StarID = new SelectList(entities.Star, "ID", "Name");

    ViewBag.Num = 7;

    return View();
}

This is my Create view:

@model Movies.Models.StarAndRole

@{
    ViewBag.Title = "Create";

    int num;

    int.TryParse(ViewBag.Num, out num);
}

<h2>Create</h2>


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

    <div class="form-horizontal">
        <h4>StarAndRole</h4>
        <h3>num is = @num</h3>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.StarID, "Star", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("StarID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StarID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <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>

        <div class="form-group">
            @Html.LabelFor(model => model.DiscID, "Disc Num", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("DiscID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.DiscID, "", new { @class = "text-danger" })
            </div>
        </div>

        <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")
}

It appears to me that everything I put in the ViewBag is NULL.

What am i doing wrong?

Thank you in advance!


Solution

  • Int.TryParse method accepts a string (representation of the int value). But you are passing a dynamic type to the method. It should give you an error about that.

    If you know you are setting an Int value from your action method, in your view, you can simply read it like

    int num = ViewBag.Num;
    

    A better solution is to add the data you want to pass to the view to your view model.

    public class YourCreateVm
    {
      public string Description { set;get;}
      public int Num { set;get;}
      public List<SelectListItem> Disks { set;get;}
      public int SelectedDiskId { set;get;}
    }
    

    and in your GET view

    public ActionResult Create()
    {
      var vm = new YourCreateVm { Num = 7 };
      vm.Disks = entities.Disc.Select(s=> new SelectListItem
                                        { Value=s.ID.ToString(), Text =s.Name}).ToList();
      return View(vm);
    }
    

    and in your view

    @model YourCreateVm
    @using(Html.BeginForm())
    {
      <p>My num is : @Model.Num <p>
      @Html.TextBoxFor(s=>s.Description)
      @Html.DropDownListFor(s=>s.SelectedDiskId, Model.Disks)
      <input type="submit" />    
    }