【问题标题】:Error binding a dropdown list in ASP.NET MVC在 ASP.NET MVC 中绑定下拉列表时出错
【发布时间】:2016-02-07 13:27:29
【问题描述】:

我正在尝试使用提供 Dealers 列表的 Web API,并尝试将其绑定到 mvc 中的 DropdownList。但是,我遇到了一个错误:

附加信息:DataBinding:“System.String”不包含名为“DealerName”的属性。

我正在使用的服务正在返回经销商详细信息列表,我必须将其显示到 mvc 中的 Web 网格中,其中包含 经销商名称Statement Month 用于搜索条件。所以,我创建了一个 ViewModel 来累积来自服务的结果,其中包含两个用于绑定到下拉列表中的附加属性。以下是代码:

Web 服务结果 - 此类的 IEnumerable 列表:

public class DealerReportResponse
{
    public string DealerCode { get; set; }
    public string DealerName { get; set; }
    public string StatementReceivedOnDate { get; set; }
    public int StatementReceivedOnDay { get; set; }
    public string StatementReceivedOnMonth { get; set; }
}

我的视图模型是:

public class DealerReportViewModel
{
    public List<string> DealerName { get; set; }
    public List<string> DealerStatementMonth { get; set; }
    public List<DealerReportResponse> DealerReportDetails { get; set; }
}

这是我将模型传递给视图的控制器:

public ActionResult Index()
{
      try
      {
          DealerReportViewModel model = new DealerReportViewModel();
          var serviceHost = //url;
          var service = new JsonServiceClient(serviceHost);
          var response = service.Get<IEnumerable<DealerReportResponse>>(new DealerReportRequest());
          if (response != null)
          {
               model.DealerName = response.Select(x => x.DealerName).Distinct().ToList();
               model.DealerStatementMonth = response.Select(x => x.StatementReceivedOnMonth).Distinct().ToList();
               model.DealerReportDetails = response.ToList();
               return View("DealerReportGrid", model);
           }
           else
           {
               //do something
           }
       }
       catch (Exception ex)
       {
            //catch exception
       }
 }

在视图中,我尝试将模型绑定到下拉列表中,如下所示:

<!-- Search Box -->
@model DealerFinancials.UI.Models.DealerReport.DealerReportViewModel
<div id="searchBox">
    @Html.DropDownListFor(m => m.DealerName, 
         new SelectList(Model.DealerName, "DealerName", "DealerName"),
         "All Categories", 
         new { @class = "form-control", @placeholder = "Category" })
</div>

但是,我无法将 DealerName 列表绑定到下拉列表。我不确定这个错误。如果我遗漏了一些要与我的模型一起传递给视图的东西,请提供帮助。

【问题讨论】:

    标签: c# asp.net asp.net-mvc model-binding html.dropdownlistfor


    【解决方案1】:

    您在生成 SelectList 时出错:您需要从 Model.DealerReportDetails 而不是从 Model.DealerName 生成它。所以而不是new SelectList(Model.DealerName, "DealerName", "DealerName") 使用 new SelectList(Model.DealerReportDetails , "DealerName", "DealerName")

    @model DealerFinancials.UI.Models.DealerReport.DealerReportViewModel
    <div id="searchBox">
        @Html.DropDownListFor(m => m.DealerName, 
             new SelectList(Model.DealerReportDetails , "DealerName", "DealerName"),
             "All Categories", 
             new { @class = "form-control", @placeholder = "Category" })
    </div>
    

    【讨论】:

    • 嗨,Alex,我尝试了代码,但它抛出了这个错误:CS1502:'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, object, System 的最佳重载方法匹配.Collections.IEnumerable)' 有一些无效参数
    • 查看更新版本。您需要使用Model.DealerReportDetails 作为选择列表的数据源
    • 成功了。感谢亚历克斯的帮助。从过去 1 小时开始就遇到问题。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多