2012-09-12 3 views
1

모델의 ID 값을 가진 필자의보기 (확인란) 필드가 있습니다. 사용자가 폼에서 체크 한 ID의 목록을 컨트롤 액션으로 반환해야합니다.mvc 컨트롤러에 모델에없는 데이터를 반환합니다.

내가 시도한 모든 것이 작동하지 않습니다. 컨트롤러로 돌아 가기 위해 코딩 된보기가 있지만 필요한 값을 반환하는 방법을 찾지 못했습니다. 이는보기 체크 박스의 단편이다

...

<td @trFormat > 
    <input id="ExportCheck" type="checkbox" value = "@item.PernrId" onclick="saveid(value);"/> 
</td> 

현재 onclick 이벤트가 ID 값을 저장해야보기 자바 스크립트 소성되어 ...

<script type="text/javascript"> 
    var keys = null; 
    function saveid(id) { 
     keys += id; 
    } 
</script> 

나는 액션 콜을 사용하여 컨트롤러로 돌아 가려고 노력했다. 현재 내가 그것을로드하는 방법을 알아낼 수 없기 때문에 다시 전송되는 어떤 경로 객체 ...

<input type="submit" value="Export to Excel" onclick="location.href='@Url.Action("ExportExcel","CastIndex")'" /> 

나는 아마이 코드를 잘못 많은 일을하고있는 중이 야 알고는 없다. 지금 막 MVC 애플리케이션으로 작업 중입니다. 어떤 도움을 주시면 감사하겠습니다. 궁극적 인 결과는 선택한 ID를 검색하고이를 내보내기로 보내기 위해 컨트롤러에 ID가 있어야한다는 것입니다. , 컨트롤러의 GET 액션에서

public class Item 
{ 
    public int Id { get; set; } 
    public string Name { get; set;} 

    //Other properties... 

    public bool Export {get; set;} //for tracking checked/unchecked 
} 

목록을 구축하고 강력한 형식의보기로 되었 :

+1

당신이 모델을 사용하지할까요? 다음과 같이하면됩니다. http://stackoverflow.com/questions/9973977/asp-net-mvc-3-retrieve-checkbox-list-values – user1477388

답변

0

는 당신과 같은 강력한 형식의 모델을 사용할 수 있습니다.

[HttpGet] 
public ActionResult MyAction() 
{ 
    var model = new List<Item>(); 

    //ToDo: Get your items and add them to the list... Possibly with model.add(item) 

    return View(model); 
} 

보기에서 HTML 도우미 "CheckBoxFor"를 사용하여 목록의 각 항목에 대한 확인란 항목을 추가 할 수 있습니다.

@using (Html.BeginForm()) 
{ 

//other form elements here 

@Html.CheckBoxFor(model=>model.Export) //this add the check boxes for each item in the model 

<input type="submit" value="Submit" /> 

} 

귀하의 목록을 소비하고 진정한 수출 가진 사람 == 찾아보실 수 있습니다 컨트롤러의 POST 액션 :

[HttpPost] 
public ActionResult MyAction (List<Item> items) 
{ 
    foreach(Item i in Items) 
    { 
    if(i.Export) 
    { 
     //do your thing... 
    } 
    } 

    //Return or redirect - possibly to success action screen, or Index action. 
} 
관련 문제