문제

액션 방법 설정이 있습니다.

public ActionResult Delete(IList<Product> products)

그리고 제 생각에 제품 테이블. 제출시 인구를 채울 수 있도록 모델 바인딩이 작동했습니다. products 목록. 그러나 확인란을 통해 선택된 제품만으로 채우고 싶습니다.

액션 방법을 변경하여 할 수 있다고 생각합니다.

public ActionResult Delete(IList<Product> products, IList<int> toDelete)

확인란 목록을 전달합니다 toDelete 그러나 가능한 경우 메소드 서명을 변경하지 않으려 고합니다.

선택한 항목 만 전달하는 방법이 있습니까? 아니면 맞춤형 모델 바인더를 작성해야합니까?

도움이 되었습니까?

해결책

왜 서명을 변경하고 싶지 않은지 이해하지 못하지만 실제로는 그렇지 않다면 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"});

다른 팁

항상 확인란 값을 사용하여 항목 삭제 여부를 표시 할 수 있습니다.

이 값의 이름은 제품 클래스의 속성과 관련이 있습니다.

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

그런 다음 delete ()에 도착하면 다음과 같은 작업을 수행 할 수 있습니다.

products = products.Where(x=>x.ToDelete == false).ToList();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top