我使用下面的代码来创建一个包含动态计数的列表。当我在我的操作中获得模型时,复选框值为null
(即使我检查它们)。
我的模特:
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" id=@("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" id=@("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" id=@("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 id=@("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" id=@("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 :(得分:1)
我测试代码并用MVC razor html Helper替换HTML
@Html.CheckBoxFor(x => Model.Job_SharhVazife[i].Asli)
并且对于所有属性并且它现在正常工作,但您必须将Job_SharhVazife
更改为List
而不是IEnumerable
和所有bool? to bool
,并且您可以添加{{1} }作为默认值。