2012-04-11 2 views
0

mvc3에서 웹 응용 프로그램을 만들고 두 개의 부분보기 에 dropdownlist와 같은 컨트롤이 있습니다. 초는 데이터베이스에서 데이터를 보여주는 webgrid가 있습니다.사전에 전달 된 모델 항목의 형식이 ''System.Collections.Generic.IEnumerable` 모델의 항목이 필요합니다.

partialview1.cshtml 
@model Mapping.Models.SecurityIdentifierMapping 

@using (Html.BeginForm("Mapping", "Home")) 
{ 
    @Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --") 
    <br />  
    @Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --") 
    <br /> 
    <button type="submit">Map</button> 
} 

partialview2.cshtml 
@model IEnumerable<Mapping.Models.SecurityIdentifierMapping> 

@{ 
    ViewBag.Title = "Mapping"; 
    WebGrid grid = null; 
    if (Model.Count() > 0){ 
    grid = new WebGrid(source: Model, 
          defaultSort: "Id", 
          canPage: true, 
          canSort: true, 
          rowsPerPage:20); 
    } 
} 


<h2>Mapping</h2> 


@if (grid != null) 
{ 
@grid.GetHtml(
       tableStyle: "grid", 
       headerStyle: "head", 
       alternatingRowStyle: "alt", 
       columns: grid.Columns(
              grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>), 
              grid.Column("PricingSecurityID"), 
              grid.Column("CUSIP") 
             ) 

       ) 
} 
<br /> 
<p> 
    @Html.ActionLink("Back", "Index") 
</p> 

in index.cshtml 


<div> 
@Html.Partial("_ControlsPartial",) 
</div> 

<div> 
@Html.Partial("_WebGridPartial") 
</div> 

HomeController.cs 
    public ActionResult Index() 
     { 
      SecurityIdentifierMapping objModel = new SecurityIdentifierMapping(); 
      objModel.PricingSecurityID = objRepository.GetPricingSecurityID(); 
      objModel.CUSIP = objRepository.GetCUSIP(); 
      return View(objModel); 

     } 

동일한 색인()으로 웹 페이지와 드롭 다운을 어떻게 표시 할 수 있습니까 ??

점점 @ Html.Partial()하도록 그리드 및 제어가 모두 같은 페이지에서 잘 작동합니다.?

+0

이 집 PLZ의 모든 MVC 전문가 나에게 어떤 힌트를 줄이이보기 모델을 작성하고이 인덱스보기로 전달합니다 컨트롤러 액션을입니까? – Neo

+0

현재 페이지의 모델은 어떤 유형입니까? – Tuan

+0

IEnumerable Neo

답변

1

당신은 Index 뷰에 SecurityIdentifierMapping 모델 전달됩니다 내부의 두 번째 파라미터해야한다 무엇 :(오류입니다. 내부

@Html.Partial("_ControlsPartial") 

과 :

@Html.Partial("_WebGridPartial") 

첫번째는 F 작동이 인덱스보기는 2 파셜를 호출 왜냐하면 강력하게 IEnumerable<SecurityIdentifierMapping>에 입력 되었기 때문에 SecurityIdentifierMapping에 강력하게 입력되었지만 두 번째 (그리드가있는 것)는 작동하지 않기 때문입니다. 따라서 예외가 발생합니다.

첫 번째 부분으로 전달할 수있는 간단한 SecurityIdentifierMapping과 두 번째 부분으로 전달할 IEnumerable<SecurityIdentifierMapping> 속성의 두 가지 속성을 포함하는보기 모델을 사용하는 것이 좋습니다.

Index.cshtml을 :

@model MyViewModel 

<div> 
    @Html.Partial("_ControlsPartial", Model.SecurityIdentifier) 
</div> 

<div> 
    @Html.Partial("_WebGridPartial", Model.Identifiers) 
</div> 
+0

1. 'Model.Identifiers'는 무엇입니까? 2. @model 문은 두 부분 뷰 및 index.cshtml 내부에서 사용해야합니까? 인덱스를 그대로 유지해야하는 이유는 무엇입니까? webgrid 데이터를 표시하려면 어떻게 선택해야합니까? – Neo

+0

'Identifiers'는'IEnumerable '형식의 뷰 모델에있는 속성입니다. 2. 부분보기 내부에서 현재와 동일한 @ 모델을 유지할 수 있습니다. 'Index.cshtml' 안에는 내 대답과 같이 @model MyViewModel을 사용할 수 있습니다. 3. webgrid를 포함하는 부분은'IEnumerable '에 강하게 입력되므로 모델을 그리드의 데이터 소스로 직접 사용할 수 있습니다. 남은 일은 컨트롤러 액션에서 뷰 모델을 인스턴스화하고 2 가지 속성을 채우는 것입니다. 즉,보기에 필요한 데이터를 준비하는 것입니다. –

+0

index() 메서드의 index.cshtml 안에 View (dbContext.SecurityIdentifierMappings.ToList()) 오류가 발생했습니다 : 사전에 전달 된 모델 항목의 형식이 'System.Collections.Generic.List '1 [Mapping.Models.SecurityIdentifierMapping] '이지만이 사전은'System.Collections.Generic.IEnumerable'1 [Mapping.Models.SecurityIdentifierMappingViewModel] '형식의 모델 항목이 필요합니다. – Neo

관련 문제