0

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.

1 Answer 1

1

You have an error generating SelectList: You need to generate it from Model.DealerReportDetails and not from Model.DealerName . So instead of new SelectList(Model.DealerName, "DealerName", "DealerName") use 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>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Alex, I tried the code but it is throwing this error: CS1502: The best overloaded method match for 'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, object, System.Collections.IEnumerable)' has some invalid arguments
See the updated version. You need to use Model.DealerReportDetails as a data-source for select list
It worked. Thanks for the help Alex. Been struck with issue from last 1 hour. Big thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.