2016-12-06 15 views
0

私は2つの部分的なビューを含むビューを持っています。MVC、標準パーシャルビュー、リストパーシャルビュー。一緒に働いていない! S.O.S. xD

@model ListViewModel 
.... 
@{   
    Html.RenderPartial("EditCartItemsPartical"); 
    Html.RenderPartial("ShowProductINFO", Model.Products); 
} 

と私はちょうど最初の部分とfromを作成し、2番目の部分のリストを作成したいと思います。

部分図

EditCartItemsPartical.cshtml

@model TestNewATM3._0.Models.CartItem 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    <div class="form-horizontal"> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.CartId, "CartId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CartId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CartId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     .... // more control for CartItem 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

ShowProductINFO.cshtml

@model IEnumerable<TestNewATM3._0.Models.AllProduct2> 
<p>@Html.ActionLink("Create New", "Create")</p> 
<table class="table"> 
    <tr> 
     <th>ID</th> 
     <th>@Html.DisplayNameFor(model => model.Title)</th> 
    </tr> 
    @foreach (var item in Model) { 
     <tr> 
      <td>@Html.DisplayFor(model => item.Id)</td> 
      <td>@Html.DisplayFor(modelItem => item.Title)</td> 
     </tr> 
    } 
</table> 

そして、私のコントローラで、私はこれを得ました。

public ActionResult Edit() 
{ 
    var db = ApplicationDbContext.Create(); 
    var list = new ListViewModel 
    { 
     Products = db.AllProduct2s.ToList(), 
     Users = db.Users.ToList(), 
     Cart = db.Carts.ToList(), 
     CartItem = db.CartItems.ToList() 
    }; 
    return View(list); 
} 

しかし、問題は、私は私の見解でlistを送信する場合、その後、私の最初の部分は、それが@model TestNewATM3.0.Models.CartItemをしたい原因の問題を取得するので、私は同時に両方の部分を示して傾けることです。しかし私がリストを送っていなければ私はそれを送らないので私のリストは表示されません。

どのようにして、通常の部分フォームビューとリスト部分ビューを同時に表示するには?

+0

'Html.RenderPartial(" EditCartItemsPartical "、新しいCartItem());'(部分ビューに 'CartItem'の新しいインスタンスを渡します)。 –

+0

[この質問/回答](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary)を読んでみてください。 -requ)あなたが得たと思われるエラーの説明を(たとえあなたがあなたの質問に含めなかったとしても) –

+0

私はそれを見ていただきありがとうございます –

答えて

0

例を理解するのが少し難しいです(作成ボタンを使用して編集ビューを表示しているなど)、カートを編集するためのビューを作成しようとしているようです。

注:分かりやすいように、モデルのCartItemプロパティの名前をCartItemsに変更することをお勧めしますが、以下の例では現在のプロパティ名を使用します。

あなたは、そのようなリストの各項目のための部分的なビューをレンダリング が、このアプローチは、より少し厄介以上であることができます:

foreach(var cartItem in Models.CartItem){ 

    Html.RenderPartial("EditCartItemsPartical", cartItem); 

} 

シンプルでよりパフォーマンスの高いアプローチが編集するだろう最初のビューそれはもちろんそう

@model IEnumerable<TestNewATM3._0.Models.CartItem> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 
<div class="form-horizontal"> 
    @foreach(var modelItem in Model.CartItem){  
     <h4>CartItem</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => modelItem.CartId, "CartId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => modelItem.CartId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => modelItem.CartId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => modelItem.ProductId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => modelItem.ProductId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Aantal, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => modelItem.Aantal, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => modelItem.Aantal, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    }  
    <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
    </div>  
</div> 
} 

同様、カート項目のコレクション

を取るので、これはSERVを変更するには、あなたが必要になりますあなたのカートのアイテムのコレクションを受け取るためのサイドコードがありますが、ユーザーの視点から、フォームを複数回転記しなくても、変更を完了するとより良い経験が得られます。

免責事項:私はメモ帳に上記の表示を変更し、それは完全に

私はこれが役に立てば幸いに動作する前にビットを調整する必要があります。

+0

ああごめんなさい。私は1つの通常のビューで2つの部分的なビューを取得しようとします。リストは 'Html.RenderPartial(" ShowProductINFO "、Model。私は他の部分的なクラッシュを追加し、それが '@model TestNewATM3._0.Models.CartItem'のようなモデルを必要としていると言っていますが、そのpatialモデルを与える方法は分かりません。 <3 –

関連する問題