0

I currently have an index page that looks like this

<script>
    window.onload = function () {
        // Function that creates the locked header bar
        MakeStaticHeader('myInput', 800, 600, 78, false);

        $("#disposalordersite").val("");    
        // Default sort order is descending
        document.getElementById("default").innerHTML = "";

        // Create the searchable dropdowns
        $(".chosen-select").chosen({ search_contains: true });    
    }
</script>


<h2>Disposal Orders</h2>

<p>@ViewBag.ErrorMessage</p>

<div id="DivRoot" align="left">
    <div style="overflow:hidden;" id="DivHeaderRow"></div>
    <div style="overflow-y:scroll;" onscroll="OnScrollDiv(this)" id="DivMainContent">
        <table class="table" id="myInput">
            <tr>
                <th>
                    <a href="javascript:void(0)" onclick="filter('orderdate', @ViewBag.page)">Date Created <span id="orderdate"></span></a>
                </th>
                <th>
                    <a href="javascript:void(0)" onclick="filter('default', @ViewBag.page)">DO ID <span id="default"></span></a>
                    <br />
                    <input id="id-filter" type="number" style="width:60px;border-radius:10px" min="0" />
                </th>
                <th>
                    <a href="javascript:void(0)" onclick="filter('site', @ViewBag.page)">Site <span id="site"></span></a><br />
                    @Html.DropDownList("Sites", null, "-- All --", htmlAttributes: new { @class = "form-control chosen-select", id = "disposalordersite", onchange = "filter('" + ViewBag.sortOrder + "', null, true)" })                                           
                </th>
            </tr>                 
        </table>
    </div>
</div>

And my index controller method looks like this

      public ActionResult Index()
        {
            int perpage = 50;

            var disposalOrders = db.disposalOrders.Include(p => p.site).Where(x => x.deleted.Equals(false)).OrderByDescending(x => x.ID);

            ViewBag.Sites = new SelectList(db.Sites.Where(x => x.Name != "").OrderBy(x => x.Name), "ID", "Site");

            ViewBag.orderby = "false";
            ViewBag.sortOrder = "default";

            ViewBag.page = 1;
            ViewBag.totalPages = (int)Math.Ceiling((float)disposalOrders.Count() / perpage);

            return View(disposalOrders.Take(perpage).ToList());
        }

But I Receive the error

DataBinding: 'System.Data.Entity.DynamicProxies.Site_029E6A67501BC68F11B69EC25ADE322F959DA21E4F149BC6829B848C7D7AB9A0' does not contain a property with the name 'Site'.

When it gets to

 @Html.DropDownList("Sites", null, "-- All --", htmlAttributes: new { @class = "form-control chosen-select", id = "disposalordersite", onchange = "filter('" + ViewBag.sortOrder + "', null, true)" })

Can someone explain what is going wrong because I'm really confused right now

chris
  • 121
  • 11

1 Answers1

0

I suspect your model name is incorrect hence not binding to the magic string, your model name and the string should match the DropDownList signature in your DropDownList

Why dont you use DropDownListFor its a better option, and you can see an example here


Change to, thats a magic string not binding.

 @Html.DropDownList("Site", null, "-- All --", htmlAttributes: new { @class = "form-control chosen-select", id = "disposalordersite", onchange = "filter('" + ViewBag.sortOrder + "', null, true)" })

Here is the ref link

Transformer
  • 6,963
  • 2
  • 26
  • 52
  • 1
    Thanks you were close, but I fixed it by doing ViewBag.Sites = new SelectList(db.Sites.Where(x => x.Name != "").OrderBy(x => x.Name), "ID", "Name"); I'll look into using DropDownListFor for future reference – chris Apr 07 '20 at 14:21
  • @chris can you please mark as answer if you find it useful. Also feel free to edit my answer, click on edit its a community form :) . – Transformer Apr 08 '20 at 05:15