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

时间:2015-04-21 04:38:43

标签: jquery asp.net-mvc post ilist

我有两个模型如下

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为表#diagnosis生成动态行并添加值,但在回发“provider_diagnosis_dtls”时,“Provider_preapproval”模型中的Ilist将变为空。

enter image description here

除了这个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
                   });

                    });

1 个答案:

答案 0 :(得分: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>