2012-05-11 2 views
1

내 VIEW에 다음 코드가 있고 그 다음에 제출 버튼이 있습니다. 내보기에는 이러한 확인란이 너무 많아 사용자가 원하는만큼 클릭 할 수 있습니다. 제 컨트롤러체크 박스 값이 htmlhelpers를 사용하여 컨트롤러로 돌아 가기

@Html.CheckBox("code" + l_code, false, new { Value = @item.expertiseCode }) 

난 FLL있다. 상기 HTTPPost 방법을

public ActionResult RegisterSP(RegisterModel model, FormCollection collection) 

그러나, 어떤 디버깅 난 ALL 체크 박스가 컨트롤러에 다시 전달하고, 단지되는 것을 볼 때 클릭 한 것. 나는 단지 클릭 한 것들을 원하고 나머지는 DB에 추가 할 필요가 있기 때문에 무시한다. 또한 전달 된 Checbox 값에는 TRUE/FALSE가 포함됩니다. 이 때문에 잘못된 값도 DB에 추가됩니다. 내가 아래의 방법을 사용하는 경우 (htmlHelper를 사용하지 않음), 위의 문제가 없습니다.

<input type="checkbox" name="[email protected](l_code)" value="@item.expertiseCode" /> 
+0

H 아, 당신의 체크 박스가 만들어 졌나요? –

답변

0

public class ExpertiseCodeViewModel 
{ 
    public string Name { set;get;} 
    public int ExpertiseId { set;get;} 
    public bool IsSelected { set;get;} 
} 
같은 뷰 모델을 당신이 체크 박스의 컬렉션을 한 경우,

@Html.CheckBox("code" + l_code, false, new { @value = item.expertiseCode }) 

또는

string name = "code" + l_code; 
@Html.CheckBox(name, false, new { @value = item.expertiseCode }) 
1

을 시도 만들기 :하지만 난 Html 헬퍼를 사용하려면 WUD

이제 기본 ViewModel에서 다음을 추가하십시오. 속성

public class UserViewModel 
{ 
    public List<ExpertiseCodeViewModel > Expertises{ set; get; } 

    public UserViewModel() 
    { 
    if(this.Expertises==null) 
     this.Expertises=new List<ExpertiseCodeViewModel>(); 
    } 
} 

그리고에서와 같이이 수집하여 ExpertiseCodeViewModel

@model ExpertiseCodeViewModel 
@Html.CheckBoxFor(x => x.IsSelected) 
@Html.LabelFor(x => x.IsSelected, Model.Name) 
@Html.HiddenFor(x => x.ExpertiseId) 

라는 편집기 템플릿을 만들

, 당신의 HTTPPost의 조치 방법에서 자신의 메인보기

@model UserViewModel 
@using (Html.BeginForm()) 
{ 
    //other elements 
@Html.EditorFor(m=>m.Expertises) 
<input type="submit" value="Save" /> 
} 

이 포함

[HttpPost] 
public ActionResult Save(UserViewModel model) 
{ 
    List<int> items=new List<int>(); 
    foreach (ExpertiseCodeViewModel objItem in model.Expertises) 
    { 
    if (objPVM.IsSelected) 
    { 
     //you can get the selected item id here 
     items.Add(objItem.ExpertiseId); 

    } 
    } 
} 
관련 문제