選択したアイテムのみのIListのを結合モデル
-
19-09-2019 - |
質問
私はアクションメソッドを設定している。
public ActionResult Delete(IList<Product> products)
そして、私の見解では製品のテーブル。私はproducts
リストを読み込むことができ提出するように取り組んで結合モデルを持っています。しかし、私は、チェックボックスを介して、選択されている製品のみを移入したいと思います。
私はこれにアクションメソッドを変更することでそれを行うことができると思います:
public ActionResult Delete(IList<Product> products, IList<int> toDelete)
そしてtoDelete
にチェックボックスのリストを渡すことが、私は本当に可能な場合メソッドのシグネチャを変更しないようしたいと思います。
選択したアイテムのみを渡す方法はありますか?それとも私は、カスタムModelBinderを記述しているつもりですか?
解決
あなたが署名を変更したくない理由を私は理解していないが、あなたが本当にない場合は、単にアクセスのViewData [「toDelete」]または
int[] toDelete;
UpdateModel(toDelete, "toDelete");
または
public class FormViewModel {
IList<Product> Products {get;set;}
int[] ToDelete {get;set;}
}
var viewmodel = new FormViewModel();
UpdateModel(viewmodel, new[]{"Products", "ToDelete"});
他のヒント
あなたは、常に項目を削除するかどうかを示すために、チェックボックスの値を使用することができます。
この値の名前は、あなたのProductクラス内のプロパティに関連するでしょう。
<form>
<% for(int i = 0; i < products.Count) { %>
<div>
<input type="hidden" name='<%=string.Format("products[{0}].Property1", i) %>' value='<%= products[i].Property1 %>' />
<input type="hidden" name='<%=string.Format("products[{0}].Property2", i) %>' value='<%= products[i].Property2 %>' />
<input type="hidden" name='<%=string.Format("products[{0}].Property3", i) %>' value='<%= products[i].Property3 %>' />
<input type="checkbox" name='<%=string.Format("products[{0}].ToDelete", i) %>' value='true' />
</div>
<% } %>
</form>
次に、あなたは(あなたの削除を取得する場合)、次のような何かを行うことができます
products = products.Where(x=>x.ToDelete == false).ToList();
所属していません StackOverflow