0
votes

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.

1
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.
What you claiming is simply not possible. Your model in the view is @model IEnumerable<Leading> but the message is clearly telling your that its Leading (not IEnumerable). And if it was @model IEnumerable<Leading> then your code inside the <form> would be throwing other errors. You have not shown us the correct code - user3559349
@CraigW., That would be throwing this error - user3559349
In fact the message states that the model is LeadingSires (which is nothing at all to do with the code you have shown) - user3559349
@CraigW., I have added the Controller. Please have a look. Thank you. - Raj

1 Answers

1
votes

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>