动态生成的行详细信息未回发

Dynamically generated row details is not posting back

我有两个型号如下

public partial class provider_preapproval
{
public string encounter_type { get; set; }
public DateTime? e_start_date { get; set; }
public DateTime? e_end_date { get; set; }
public string status { get; set; }
public virtual IList<provider_diagnosis_dtls> provider_diagnosis_dtls { get; set; }
}

public partial class provider_diagnosis_dtls
{
    public string diagnosis_code { get; set; }
    public string diagnosis_desc { get; set; }
    public string diagnosis_level { get; set; }
}

相同的视图是

@using (Html.BeginForm("Preapprove", "Preapproval", FormMethod.Post)
{
 @Html.LabelFor(model => model.encounter_type, htmlAttributes: new { id = "n", @class = "control-label" })                                                
@Html.DropDownListFor(model => model.encounter_type, (SelectList)@ViewBag.Encounter, "---Select Encounter Type ---", new { @class = "m-wrap " 
 @Html.LabelFor(model => model.status, htmlAttributes: new { id = "n", @class = "control-label" })                                                @Html.TextBoxFor(model => model.status, new { id = "status", @Value = "New", @readonly = "readonly" })
 @Html.LabelFor(model => model.e_start_date, htmlAttributes: new { id = "e_start", @class = "control-label " })                                                @Html.TextBoxFor(model => model.e_start_date, new { id = "start", @class = "m-wrap  ", @Value = @TempData["service_date"], @readonly = "readonly" })
 @Html.LabelFor(model => model.e_end_date, htmlAttributes: new { id = "e_end", @class = "control-label " })                                                @Html.TextBoxFor(model => model.e_end_date, new { id = "end", @class = "m-wrap  datepicker", @Value = @TempData["service_date           

//Template for dynamic generating row                         
<table id="Newdiagnosis" style="display:none">
<tr>
<td><input  class="diag" style="width:200px" type="text" name="provider_diagnosis_dtls[#].diagnosis_code" value /></td>
<td><input class="diag_desc" style="width:500px" type="text" name="provider_diagnosis_dtls[#].diagnosis_desc" value /></td>
<td>
<input  type="text" name="provider_diagnosis_dtls[#].diagnosis_level)" readonly value />
<input type="hidden" name="diagnosis.Index" value="%" />
</td>
</tr>
</table>
 <table id="diagnosis" class="table table-striped table-hover table-bordered">
@if (Model != null)
{
for (int i = 0; i < Model.provider_diagnosis_dtls.Count; i++)
{
<tr>
 <td>@Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_code)</td>
 <td>@Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_desc)</td>
                                                                                                  @Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_level, new { @Value = "Primary", @readonly = "readonly" })                                                <input type="hidden" name="diagnosis.Index" value="@i" />
                                            </td>
                                        </tr>
}
</table>
}

现在我可以使用 jquery 为 table #diagnosis 生成动态行并添加值,但是在 "Provider_preapproval" 中回发 "provider_diagnosis_dtls" Ilist模型为空。

除了这个Ilist之外的细节来了。

请告诉我我的代码有什么问题。

已编辑

Jquery

 $(document).ready(function () {
 $("#N").click(function () {

var index = (new Date()).getTime();
var clone = $('#Newdiagnosis').clone();
clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
var html = clone.html();
$("#diagnosis").append(clone.html());
                 $("#diagnosis").find(".diag").last().tokenInput("@Url.Action("SearchDiagnosis","Preapproval")",
                    {
                        theme: 'facebook',
                        preventDuplicates: true,
                        searchingText: 'Searching...',
                        tokenLimit: 1
                    });

       $("#diagnosis").find(".diag_desc").last().tokenInput("@Url.Action("SearchDiagnosis_desc","Preapproval")",
                   {
                      theme: 'facebook',
                      preventDuplicates: true,
                      searchingText: 'Searching...',
                      tokenLimit: 1
                   });

                    });

模板中索引器的隐藏输入名称错误(与您的属性不匹配。需要

<input type="hidden" name="provider_diagnosis_dtls.Index" value="%" /> 

请注意,您还需要在 for 循环中更改它

<tr>
    <td>@Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_code</td>
    <td>@Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_desc</td>
    <td>
        @Html.TextBoxFor(m => m.provider_diagnosis_dtls[i].diagnosis_level, new { @Value = "Primary", @readonly = "readonly" })
        <input type="hidden" name="m.provider_diagnosis_dtls.Index" value="@i" /> // change this
    </td>
</tr>