在 ASP.NET MVC 中绑定下拉列表时出错
Error binding a dropdown list in ASP.NET MVC
我正在尝试使用提供 经销商 列表的 Web API,并尝试将其绑定到 mvc 中的 DropdownList
。但是,我遇到了一个错误:
Additional information: DataBinding: 'System.String' does not contain a property with the name 'DealerName'.
我正在使用的服务正在返回经销商详细信息列表,我必须将其显示到 mvc 中的 Web 网格中,其中包含 经销商名称 和 Statement Month 作为搜索条件。因此,我创建了一个 ViewModel 来累积服务的结果,其中有两个附加属性用于绑定到下拉列表中。以下是代码:
Web 服务结果 - class:
的一个 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 列表绑定到下拉列表。我不确定这个错误。如果我缺少与我的模型一起传递给视图的内容,请提供帮助。
您在生成 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>
我正在尝试使用提供 经销商 列表的 Web API,并尝试将其绑定到 mvc 中的 DropdownList
。但是,我遇到了一个错误:
Additional information: DataBinding: 'System.String' does not contain a property with the name 'DealerName'.
我正在使用的服务正在返回经销商详细信息列表,我必须将其显示到 mvc 中的 Web 网格中,其中包含 经销商名称 和 Statement Month 作为搜索条件。因此,我创建了一个 ViewModel 来累积服务的结果,其中有两个附加属性用于绑定到下拉列表中。以下是代码:
Web 服务结果 - class:
的一个 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 列表绑定到下拉列表。我不确定这个错误。如果我缺少与我的模型一起传递给视图的内容,请提供帮助。
您在生成 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>