2016-08-04 140 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空,這是錯誤

值「0 '對於包含無效。

並且所有複選框(選中或未選中)都具有「false」的值。

回答

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()); 
    } 

} 

使用razor語法從您的控制器傳遞到您的視圖。

@model IEnumerable<App.Model.Data> 

使用剃刀複選框綁定到你的模型

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

一般情況下,我覺得它更容易使用的HTML /引導,少的jQuery更剃刀。沒有看到你的模型或控制器就很難解決你的複選框問題,但如果你關注MVC的基礎知識,我認爲你的許多問題都會被解決。對不起,這個答案不是很具體,但如果你修改你的問題,我可以更具體。