2016-11-14 66 views
0

我使用下面的代码来创建一个包含动态计数的列表。当我在我的动作中得到模型时,复选框值为null(即使当我检查它们时)。如何获得asp mvc中列表的复选框值

我的模型:

public partial class Job_SharhVazife 
{ 
    public int ID { get; set; } 
    public Nullable<int> Job_Id { get; set; } 
    public string Name { get; set; } 
    public Nullable<bool> Asli { get; set; } 
    public Nullable<bool> Mostamar { get; set; } 
    public Nullable<int> Dore { get; set; } 
    public Nullable<bool> Tafviz { get; set; } 

    public virtual Job_MainJob Job_MainJob { get; set; } 
    public virtual Job_SharhVazife_Dore Job_SharhVazife_Dore { get; set; } 
} 
public class AllDataOfMainJob 
{ 
    public IEnumerable<Job_SharhVazife> Job_SharhVazife { get; set; } 
} 

我的html代码:

@model JobWebApplication.Models.AllDataOfMainJob 
... 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Job_SharhVazife, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <div class="table-responsive"> 
        <table id="tblSharhVazife" class="table"> 
         <thead> 
          <tr> 
           <th>@Html.Label("Row")</th> 
           <th>@Html.Label("Header1-text")</th> 
           <th>@Html.Label("Header2-checkbox")</th> 
           <th>@Html.Label("Header3-checkbox")</th> 
           <th>@Html.Label("Header4-dropdown")</th> 
           <th>@Html.Label("Header5-checkbox")</th> 
          </tr> 
         </thead> 
         <tbody> 
          @if (Model.Job_SharhVazife != null) 
          { 
           for (int i = 0; i < Model.Job_SharhVazife.Count(); i++) 
           { 
           <tr> 
            <td>@((i + 1).ToString())</td> 
            <td><input class="form-control text-box single-line valid" [email protected]("Job_SharhVazife_" + @i + "__Name") name="Job_SharhVazife[@i].Name" type="text" value="@Model.Job_SharhVazife.ToList()[i].Name"></td> 
            <td> 
             <div class="checkbox"> 
              <label> 
               <input type="checkbox" [email protected]("Job_SharhVazife_" + @i + "__Asli") name="Job_SharhVazife[@i].Asli" value="" @if (!string.IsNullOrEmpty(Model.Job_SharhVazife.ToList()[i].Asli.ToString()) && bool.Parse(Model.Job_SharhVazife.ToList()[i].Asli.ToString())) 
               { <text> checked="checked" </text> } /><span class="text"></span> 
              </label> 
             </div> 
            </td> 
            <td> 
             <div class="checkbox"> 
              <label> 
               <input type="checkbox" [email protected]("Job_SharhVazife_" + @i + "__Mostamar") name="Job_SharhVazife[@i].Mostamar" value="" @if (!string.IsNullOrEmpty(Model.Job_SharhVazife.ToList()[i].Mostamar.ToString()) && bool.Parse(Model.Job_SharhVazife.ToList()[i].Mostamar.ToString())) 
               { <text> checked="checked" </text> } /><span class="text"></span> 
              </label> 
             </div> 
            </td> 
            <td> 
             <select [email protected]("Job_SharhVazife_" + @i + "__Dore") name="Job_SharhVazife[@i].Dore" width="100% !important" class="valid"> 
               @Html.Raw(GetSharheVazifeDoreDDL(Model.Job_SharhVazife.ToList()[i].Dore.ToString())) 
              </select> 
             </td> 
             <td> 
              <div class="checkbox"> 
               <label> 
                <input type="checkbox" [email protected]("Job_SharhVazife_" + @i + "__Tafviz") name="Job_SharhVazife[@i].Tafviz" value="" @if (!string.IsNullOrEmpty(Model.Job_SharhVazife.ToList()[i].Tafviz.ToString()) && bool.Parse(Model.Job_SharhVazife.ToList()[i].Tafviz.ToString())) 
                { <text> checked="checked" </text> } /><span class="text"></span> 
               </label> 
              </div> 
             </td> 
            </tr> 
            } 
           } 
          </tbody> 
         </table> 
        </div> 
        <input id="btnAddSharhVazife" type="button" value="+" onclick="AddSharhVazife();" /> 
        @Html.ValidationMessageFor(model => model.Job_SharhVazife, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

... 
    <script> 
     function AddSharhVazife() { 
       var m = $('#tblSharhVazife tbody tr:last-child input:last-child').attr('name'); 
       var index = 0; 
       if (m != null && m.length > 0) { 
        index = m.split('Job_SharhVazife[')[1].replace('].Name', ''); 
        index++; 
       } 

       var tableBody = $('#tblSharhVazife tbody'); 
       var htmlSharhVazifeDoreItems = '@Html.Raw(SharhVazifeDoreItems)'; 

       var html = "<tr>" + 
        "<td>" + (index+1).toString() + "</td>" + 
        "<td><input class=\"form-control text-box single-line valid\" id=\"Job_SharhVazife_" + index + "__Name\" name=\"Job_SharhVazife[" + index + "].Name\" type=\"text\" value=\"\"></td>" + 
        "<td><div class=\"checkbox\"><label><input type=\"checkbox\" id=\"Job_SharhVazife_" + index + "__Asli\" name=\"Job_SharhVazife[" + index + "].Asli\" value=\"1\" /><span class=\"text\">اصلی </span></label></div></td>" + 
        "<td><div class=\"checkbox\"><label><input type=\"checkbox\" id=\"Job_SharhVazife_" + index + "__Mostamar\" name=\"Job_SharhVazife[" + index + "].Mostamar\" value=\"2\" /><span class=\"text\"></span></label></div></td>" + 
        "<td><select id=\"Job_SharhVazife_" + index + "__Dore\" name=\"Job_SharhVazife["  + index + "].Dore\" width=\"100% !important\" class=\"valid\">" + htmlSharhVazifeDoreItems + "</select></td>" + 
        "<td><div class=\"checkbox\"><label><input type=\"checkbox\" id=\"Job_SharhVazife_" + index + "__Tafviz\" name=\"Job_SharhVazife[" + index + "].Tafviz\" value=\"3\" /><span class=\"text\"></span></label></div></td>" + 
        "</tr>"; 
       tableBody.append(html); 
      }; 
    </script> 
+0

你可以分享Job_SharhVazife模型吗? –

+0

@Ahmed Ragheb我更新了我的问题。 –

+0

您无法为字符串类型(如字符串)创建复选框。复选框用于布尔属性。 –

回答

1

我测试的代码,我用MVC剃刀HTML助手代替HTML

@Html.CheckBoxFor(x => Model.Job_SharhVazife[i].Asli) 

,并为所有属性和它现在正在工作,但是您必须将Job_SharhVazife更改为List而不是IEnumerable和所有bool? to bool,您可以将false添加为默认值。

+0

@ mj-y是这项工作?! –

+0

@Stephen你看过我的回答吗?我建议改变布尔?以默认值和IEnumerable列出。 –

+0

对不起,我错过了'布尔?到bool位:) –