我使用下面的代碼來創建一個包含動態計數的列表。當我在我的動作中得到模型時,複選框值爲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>
你可以分享Job_SharhVazife模型嗎? –
@Ahmed Ragheb我更新了我的問題。 –
您無法爲字符串類型(如字符串)創建複選框。複選框用於布爾屬性。 –