0

I am trying to use this @Html.DropDownListFor for filtering the results.

Controller:

[HttpGet]
public ActionResult Leading()
{
    ReportClassHandle ReportClassHandle = new ReportClassHandle();
    return View(ReportClassHandle.LeadingAll());
}

And LeadingAll method is:

public List<Leading> LeadingAll(string Type) 
{ 
    clsUtilities clsUtilities = new clsUtilities(); 
    DataSet ds; 
    List<Leading> leading = new List<Leading>(); 
    string sSQL; 

    SqlParameter[] prms = new SqlParameter[1]; 
    sSQL = "exec GetLeading @Type"; 
    prms[0] = new SqlParameter("@Type", SqlDbType.VarChar); 
    prms[0].Value = Type; 
    ds = clsUtilities.CreateCommandwithParams(sSQL, prms); 
    DataTable dataTable = ds.Tables[0]; 
    foreach(DataRow dr in dataTable.Rows) 
    { 
        leading.Add(new Leading
        { 
            RankId = Convert.ToInt32(dr["RankId"]), 
            Name = Convert.ToString(dr["Name"]),
        }); 
    } 

My complete Leading View is:

@model IEnumerable<ReportProject.Models.Leading>
@using (Html.BeginForm("Leading", "Home", FormMethod.Post))
{
    @Html.DisplayFor(m => m.Type) 
    @Html.DropDownListFor(m => m.Type, new List<SelectListItem> {
        new SelectListItem{ Text="General", Value="1" }, 
        new SelectListItem{Text="Advance", Value="2"}}, "Please select") 
    @Html.ValidationMessageFor(m => m.Type, "", new { @class = "error" })             
}
<table class="table table-striped">
    <tr>
        <th>@Html.DisplayName("Rank")</th>
        <th>@Html.DisplayName("Name")</th>   
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.RankId)</td>
            <td>@Html.DisplayFor(modelItem => item.Name)</td>
        </tr>
    }
</table>

My Leading Model:

public class Leading 
{ 
    public int RankId { get; set; } 
    public string Name { get; set; } 
    [Required(ErrorMessage = "Type is Required")] 
    public string Type { get; set; } 
}

When I run this code, it is throwing error:

Compiler Error Message: CS1061: 'IEnumerable<Leading>' does not contain a definition for 'Type' and no extension method 'Type' accepting a first argument of type 'IEnumerable<Leading>' could be found (are you missing a using directive or an assembly reference?)

Please guide me.

Raj
  • 555
  • 1
  • 12
  • 31
  • Please show the controller code that returns this view. My guess is that it is not returning an IEnumerable of the model type. – Craig W. Dec 07 '17 at 00:30
  • 1
    What you claiming is simply not possible. Your model in the view is `@model IEnumerable` but the message is clearly telling your that its `Leading` (not `IEnumerable`). And if it was `@model IEnumerable` then your code inside the `
    ` would be throwing other errors. You have not shown us the correct code
    –  Dec 07 '17 at 00:31
  • @CraigW., That would be throwing [this error](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) –  Dec 07 '17 at 00:32
  • In fact the message states that the model is `LeadingSires` (which is nothing at all to do with the code you have shown) –  Dec 07 '17 at 00:37
  • @CraigW., I have added the Controller. Please have a look. Thank you. – Raj Dec 07 '17 at 00:41
  • @StephenMuecke, I have edited the question. Please have a look and please guide me. – Raj Dec 07 '17 at 00:42
  • Again, what your claiming is impossible. Put breakpoints on you methods and in the view and step through your code one line at a time to find out what line of code is throwing that error. –  Dec 07 '17 at 00:45
  • @StephenMuecke, dotted red line is showing under @Html.DisplayFor(m => m.Type) – Raj Dec 07 '17 at 00:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160666/discussion-between-stephen-muecke-and-raj). –  Dec 07 '17 at 00:48

1 Answers1

1

The error occurs because the model in your view is IEnumerable<Leading> but you attempting to create controls for a single instance of Leading. Since you wanting to use the value of the dropdownlist to filter your results, then you should start with a view model to represent what you want in the view

public class LeadingFilterVM
{
    public int? Type { get; set; }
    public IEnumerable<SelectListItem> TypeList { get; set; }
    public IEnumerable<Leading> Leadings { get; set; }
}

Then you GET method will be

[HttpGet]
public ActionResult Leading(int? type)
{
    IEnumerable<Leading> data;
    ReportClassHandle ReportClassHandle = new ReportClassHandle();
    if (type.HasValue)
    {
        data = ReportClassHandle.LeadingAll(type.Value.ToString())
    }
    else
    {
        data = ReportClassHandle.LeadingAll();
    }
    LeadingFilterVM model = new LeadingFilterVM
    {
        Type = type,
        TypeList = new List<SelectListItem>
        {
            new SelectListItem{ Text = "General", Value = "1" }, 
            new SelectListItem{ Text = "Advance", Value = "2" }
        },
        Leadings = data
    };
    return View(model);
}

And change the view to

@model LeadingFilterVM
// note the FormMethod.Get
@using (Html.BeginForm("Leading", "Home", FormMethod.Get))
{
    @Html.DisplayFor(m => m.Type) 
    @Html.DropDownListFor(m => m.Type, Model.TypeList "All types")

    <input type="submit" value="search" />
}
<table class="table table-striped">
    <thead>
        <tr>
            <th>Rank</th>
            <th>Name</th>   
            <th></th>
        </tr>
    <thead>
    <tbody>
        @foreach (var item in Model.Leadings)
        {
            <tr>
                <td>@Html.DisplayFor(m => item.RankId)</td>
                <td>@Html.DisplayFor(m => item.Name)</td>
             </tr>
        }
    </tbody>
</table>
  • How about the Leading class which I have added. Should i change public string Type { get; set; } into public int? Type {get;set;} – Raj Dec 07 '17 at 01:51
  • Give me another 10 min and I'll explain that and a few other issues in out chat :) –  Dec 07 '17 at 01:53
  • Sure. Please take your time. – Raj Dec 07 '17 at 01:55