【问题标题】:Populating @Html.DropDownListFor -- OK from hard-coded values, error from database填充@Html.DropDownListFor——硬编码值确定,数据库出错
【发布时间】:2013-12-17 17:28:34
【问题描述】:

我们在表单上填充下拉列表,内置于 C# ASP.NET MVC 4(有时是 5)。感谢 SOF,我建立了一个临时列表,如下所示:

/// <summary>
/// Generate a list of countries.
/// </summary>
/// <returns>List(SelectListItem)</returns>
/// <remarks>Values are from ISO 3166-1 alpha-3</remarks>
public static List<SelectListItem> Countries()
{
    List<SelectListItem> items = new List<SelectListItem>();

    items.Add(new SelectListItem { Text = "United States of America", Value = "USA", Selected = true });
    items.Add(new SelectListItem { Text = "Australia", Value = "AUS" });
    items.Add(new SelectListItem { Text = "Canada", Value = "CAN" });
    items.Add(new SelectListItem { Text = "Mexico", Value = "MEX" });
    items.Add(new SelectListItem { Text = "United Kingdom", Value = "GBR" });

    return items;
}

然后将其传递到 ViewBag 中:

ViewBag.CountryList = SelectLists.Countries();

并将其呈现为:

@Html.DropDownListFor( model=>model.country_code, 
    (List<SelectListItem>)ViewBag.CountryList )

这部分一切正常。

现在团队正在实现代码以从数据库而不是模拟数据中检索查找,但情况并不好。我们的业务对象方法接受查找类型,在本例中为“Country”,并应返回 List&lt;SelectListItem&gt;

控制器:

List<SelectListItem> countryList = GetLookupData( "Country" );
ViewBag.CountryList = countryList;

型号:

public static List<SelectListItem> GetLookupData(string lookupType)
{
    MPPEntities dbContext = new MPPEntities();
    var query = (from c in dbContext.SystemLookups
                    where c.lookup_type == lookupType
                    orderby c.order_by
                    select new SelectListItem { Text = c.name, Value = c.value })
                    .ToList<SelectListItem>();

    return (List<SelectListItem>)query;
}

当我们调试 LINQ 时,query 包含正确的数据。但是当调试器返回到控制器时,countryList 失败,因为“无法计算表达式”。然后当然视图本身会失败。

基于对模拟列表有效且真实列表包含正确数据的观察,我推断故障点在于从通用集合到List&lt;SelectListItem&gt; 的转换。列表类型转换的正确方法是什么?

ETA:CSHTML 文件中的错误是: “用户代码未处理 RuntimeBinderInternalCompilerException。”这是使用下面推荐的较少演员表。

【问题讨论】:

    标签: c# asp.net-mvc linq list asp.net-mvc-4


    【解决方案1】:

    你似乎做了很多无用的铸件......

    你可以试试

    public static List<SelectListItem> GetLookupData(string lookupType)
        {
           MPPEntities dbContext = new MPPEntities();
           return (from c in dbContext.SystemLookups
                        where c.lookup_type == lookupType
                        orderby c.order_by
                        select new SelectListItem { Text = c.name, Value = c.value })
                        .ToList();
        }
    

    在你看来,你能不能试试

    @{
      var contryCodes = (IEnumerable<SelectListItem>)ViewBag.CountryList;
    }
    
    @Html.DropDownListFor( model=>model.country_code, 
        countryCodes )
    

    因为它看起来像是动态(ViewBag)的问题......

    【讨论】:

    • 不,我得到了和以前一样的RuntimeBinderInternalCompilerException
    • 没用。转换为视图变量会引发异常。
    • 我确定问题不在视图中,因为当我在 ViewBag 中传递我的模拟列表时视图正确呈现。问题必须是我想放入包中的东西,而不是我实际放入包中的东西。
    • 现在,一个月后,这种方法非常有效。
    • @CodeswithHammer 始终保持耐心 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多