2012-04-11 12 views
0

私はmvc3でWebアプリケーションを作成し、ドロップダウンリストのようなコントロールを持つ2つの部分ビュー を作成しました。 秒はデータベースからのデータを表示する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); 

     } 

webgridを表示し、同じIndex()でプルダウンする方法はありますか?

グリッドと制御の両方が同じページ上で正常に動作するようにエラー:( @ Html.Partialの内側に第二パラメータがどうあるべきかを()取得。?

+0

ハウスプラザ内の任意のMVCの専門家は私に任意のヒントを与えますか? – Neo

+0

現在のページのモデルはどのようなタイプですか? – Tuan

+0

IEnumerable Neo

答えて

1

あなたはIndexビューにSecurityIdentifierMappingモデルを渡している。インサイド最初のものはfで働く

@Html.Partial("_WebGridPartial") 

@Html.Partial("_ControlsPartial") 

と:この指数は、あなたが2つのパーシャルを呼び出す見ますこれは強く型付けされたSecurityIdentifierMappingですが、2番目の型(グリッド付きの型)は強く型付けされているので動作しません。IEnumerable<SecurityIdentifierMapping>です。したがって、あなたが得ている例外。

2つのプロパティを含むビューモデルを使用することをお勧めします.1つは単純なSecurityIdentifierMappingで、最初の部分に渡すことができ、IEnumerable<SecurityIdentifierMapping>プロパティは2番目の部分に渡すことができます。これは、このビューモデルを記入し、Indexビューに渡しますコントローラのアクションです:

Index.cshtml

@model MyViewModel 

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

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

1.「Model.Identifiers」は何ですか? 2. @modelステートメントは、2つの部分的なビューと内部の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

関連する問題