2016-08-04 2 views
-1

저는 Ajax를 사용하여 컨트롤러에서 정보를 검색하고이를 내보기에 체크 박스 목록으로 표시했습니다.체크 박스가 컨트롤러에 바인딩되어 있지 않습니다.

$(document).ready(function() { 
    $('#submitButton').hide(); //hide some buttons 
    $("#Name").hide(); 
    $("#Contact").hide(); 
    $("#Desc").hide(); 
    $("#PMeeting").hide(); 
    $("#Params").hide(); 

    $('#SelectedTeam').change(function() { 
     $('#content').html(''); 
     $.ajax({ 
      url: '/Audits/GetAuditParams', //this function retrieves a list of objects 
      type: "POST", 
      dataType: "json", 
      data: { 
       'tn': $('#SelectedTeam').val(), 
      }, 
      success: function (data) { //here I create the table with checkboxes and labels 
       $.each(data, function (i, item) { 
        var li = $('<input type="checkbox" value="' + item.Included + '" name=Parameters[' + i + '].Included id=Parameters_' + i + '__Included"/>' + 
      '<label for="Parameters[' + i + ']"></label></br>'). 
         text(item.ParameterDescription).prop('checked', item.Included); 

        li.find('label').text(item.ParameterDescription);//I create a set of hiddem fields with the same new, otherwise the collection will be null in the controller 

        $('<input>').attr({ 
         type: 'hidden', 
         id: 'Parameters_' + i + '__Included', 
         name: 'Parameters[' + i + '].Included' 
        }).appendTo('form'); 
        $('#content').append(li); 
       }); 
      } 
     }); 
     $.ajax({ //this is for a different information 
      url: '/Audits/GetAuditInfo', 
      type: "POST", 
      dataType: "json", 
      data: { 
       'tn': $('#SelectedTeam').val(), 
      }, 
      success: function (data) { 
       $("#SProject_ProjectName").val(data.ProjectID); 
       $("#SProject_POC").val(data.POC); 
       $("#SProject_PDescription").val(data.PDescription); 
       $("#SProject_PeriodicMeeting").val(data.PeriodicMeeting); 
       $("#Name").show(); 
       $("#Contact").show(); 
       $("#Desc").show(); 
       $("#PMeeting").show(); 
       $("#Params").show(); 

      } 
     }); 
     $('#submitButton').show(); 

    }); 

    function isChecked(value) { 

     if (value == 1) { 
      return true; 
     } 
     else 
      return false; 
    } 

    $('form').submit(function (e) { 
     $('input[type=checkbox]').prop('checked', function (index, value) { 
      if (value == true) { 
       $('#Parameters_' + index + '__Included').val(1); 
       $('#Parameters_' + index + '__Included').prop('checked', "checked"); 
      } else { 
       $('#Parameters_' + index + '__Included').val(0); 
      } 

     }); 
    }); 
}); 

이 내 HTML 코드

<html> 
<head> 
</head> 
<body> 
    <div class="col-md-4" id="content"></div> 
</body> 
</html> 

하지만 체크 박스 컨트롤러, ModelStated.isvalid = false에 널 (null)을 위해 나는 정보를 얻고,이 오류

값 '0 '은 (는) 포함 된 항목에 대해 유효하지 않습니다.

그리고 체크 박스 (선택 또는 선택 취소)의 값은 모두 "거짓"입니다.

답변

2

좀 더 자세한 정보를 제공하고 진술 문구를 특정 질문 형태로 다시 말해야합니다. 그것 이외에, 나는 그것이 빈 HTML 페이지처럼 보이는 모든 코드라고 생각하지 않는다. MVC 컨트롤러에서 데이터를 가져 오려면 컨트롤러에서 사용자의보기에 제공 할 모델을 만들어야합니다.

Accessing your model's data from a controller - asp.net

는 기본적으로, 당신은 다음 컨트롤러 컨텍스트의 인스턴스를 생성, 모델 및 데이터베이스 컨텍스트를 만들 : 여기 당신이 할 수있는 방법의 예입니다.

public class YourController : Controller 
{ 
    private YourDBContext db = new YourDBContext(); 
    public ViewResult Index() 
    { 
     return View(db.YourData.ToList()); 
    } 

} 

면도기 구문을 사용하여 컨트롤러에서보기로 전달합니다.

@model IEnumerable<App.Model.Data> 

사용 면도기 나는 쉽게 HTML/부트 스트랩 적은 jQuery로 더 면도기를 사용하는 것이 발견, 일반적으로 모델

@Html.CheckBoxFor(model => model.data) 

에 체크 박스를 결합합니다. 모델이나 컨트롤러를 보지 않고도 체크 박스 문제를 해결하는 것은 어렵지만, MVC의 기초에 집중한다면 많은 문제가 정리 될 것이라고 생각합니다. 이 답변은 매우 구체적이지는 않지만 유감스럽게 생각합니다. 질문을 수정하면 더욱 구체적이 될 수 있습니다.

관련 문제