2017-12-07 7 views
0

결과를 필터링하는 데이 @Html.DropDownListFor을 사용하려고합니다.IEnumerable '에'Type '에 대한 정의가 없습니다.

컨트롤러 :

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

그리고 LeadingAll 방법은 다음과 같습니다

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"]), 
     }); 
    } 

Leading View가 완료 내 :

@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> 

Leading Model :

나는이 코드를 실행, 오류를 던지고있다 :

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?)

이 나를 인도 해주십시오.

+0

으로 변경하십시오.이보기를 반환하는 컨트롤러 코드를 표시하십시오. 내 생각 엔 IEnumerable 모델 유형을 반환하지 않는다는 것입니다. –

+1

귀하가 주장하는 것은 단순히 가능하지 않습니다. 뷰의 모델은'@model IEnumerable '이지만 메시지는 명확하게'Leading' ('IEnumerable'이 아니라)이라고 말하고 있습니다. 그리고 만약 그것이'@model IEnumerable '이면'

'안에있는 코드는 다른 에러를 던질 것입니다. 올바른 코드 –

+0

@CraigW를 표시하지 않았습니다. [이 오류] (https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-http : –

답변

1

보기의 모델이 IEnumerable<Leading>이고 Leading의 단일 인스턴스에 대한 컨트롤을 만들려고하기 때문에 오류가 발생합니다. 당신이 당신의 결과를 필터링하는 드롭 다운리스트의 값을 사용하고자하기 때문에, 당신은 방법이 될 것입니다

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

그런 다음 당신이 얻을보기에 당신이 원하는 것을 표현하기 위해 뷰 모델로 시작해야

[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); 
} 

보기를

@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> 
+0

내가 추가 한 Leading 클래스는 어떻습니까? 공용 문자열을 변경해야합니다. Type {get; 세트; } public int로? 타입 {get; set;} – Raj

+0

또 10 분을 줘. 나는 채팅에서 다른 문제를 설명 할게 :) –

+0

물론. 시간을 내주십시오. – Raj

관련 문제