2016-08-04 138 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的基础知识,我认为你的许多问题都会被解决。对不起,这个答案不是很具体,但如果你修改你的问题,我可以更具体。