2016-11-14 28 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

stringのような文字列型のチェックボックスを作成することはできません。チェックボックスはブール値のプロパティです。 –

答えて

1

私は、コードをテストし、私はMVCかみそりHTMLヘルパー

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

し、すべてのプロパティのためにHTMLを交換し、今働いていますが、Job_SharhVazifeListに変更する必要はありません。IEnumerableとすべてbool? to boolであり、デフォルト値としてfalseを追加できます。

+0

@ mj-yはこの作品ですか! –

+0

@Stephenあなたは私の答えを読んだのですか?私はブールを変更することをお勧めしますか?デフォルト値でboolに、IEnumerableでリストに追加します。 –

+0

申し訳ありませんが、私は 'bool?ブール 'に:) –

関連する問題