【发布时间】: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