I'm trying to consume a Web API that gives the list of Dealers and trying to bind it into a DropdownList in mvc. However, I'm facing an error:
Additional information: DataBinding: 'System.String' does not contain a property with the name 'DealerName'.
The service I'm consuming is returning the list of Dealer details and I've to display that into a web grid in mvc with two dropdown lists of Dealer Name and Statement Month for search criteria. SO, I have created a ViewModel to accumulate the result from service with two addtional properties to be used to bind in the dropdownlist. Following is the code:
Web Service Result - An IEnumerable List of this class:
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; }
}
My View Model is:
public class DealerReportViewModel
{
public List<string> DealerName { get; set; }
public List<string> DealerStatementMonth { get; set; }
public List<DealerReportResponse> DealerReportDetails { get; set; }
}
Here is the Controller where I'm passing the model to the View:
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
}
}
And In the View, I'm trying to bind the model into a dropdown list as follows:
<!-- 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>
However, I'm not able to bind the DealerName list to a dropdown list. I'm not sure about the error. Please help if I'm missing something to pass along with my model to the View.