【问题标题】:C# - Receiving DropDownList Data Binding errorC# - 接收 DropDownList 数据绑定错误
【发布时间】:2020-04-06 22:11:47
【问题描述】:

我目前有一个如下所示的索引页面

<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>

我的索引控制器方法看起来像这样

      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());
        }

但我收到错误提示

DataBinding:“System.Data.Entity.DynamicProxies.Site_029E6A67501BC68F11B69EC25ADE322F959DA21E4F149BC6829B848C7D7AB9A0”不包含名为“Site”的属性。

到时候

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

谁能解释一下出了什么问题,因为我现在真的很困惑

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

    我怀疑您的模型名称不正确,因此没有绑定到魔术字符串,您的模型名称和字符串应该匹配您的 DropDownList 中的 DropDownList 签名

    你为什么不使用DropDownListFor,它是一个更好的选择,而你can see an example here


    更改为,那是一个没有绑定的魔术字符串。

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

    这里是ref link

    【讨论】:

    • 谢谢你很亲密,但我通过 ViewBag.Sites = new SelectList(db.Sites.Where(x => x.Name != "").OrderBy(x => x .姓名)、“身份证”、“姓名”);我会考虑使用 DropDownListFor 以供将来参考
    • @chris 如果您觉得有用,请标记为答案。也可以随意编辑我的答案,点击编辑它的社区表单:)。
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多