行不会在MVC中回发

时间:2015-07-07 01:46:43

标签: jquery asp.net-mvc

在项目中有两种模型,如下所示

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; }
[InverseProperty("preapproval")]
public virtual IList<provider_service_dtls> provider_service_dtls { get; set; }
}

 public partial class provider_service_dtls
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long service_id { get; set; }
        public long preapproval_id { get; set; }
        public string activity_code_type { get; set; }
        public string activity_type { get; set; }
        public string activity_type_name { get; set; }
        public string activity_code { get; set; }
        public string activity_name { get; set; }
        public string internal_activity_code { get; set; }
        public string internal_activity_name { get; set; }
        public DateTime? activity_start_date { get; set; }
        public decimal? net_amt { get; set; }
        public decimal? unit_amt { get; set; }
        public decimal? quantity { get; set; }
        public string clinician { get; set; }
        public string clinician_code { get; set; }
        [ForeignKey("preapproval_id"), InverseProperty("provider_service_dtls")]
        public virtual provider_preapproval preapproval { get; set; }

    }
}

视图cshtml

  @using (Html.BeginForm("Preapprove", "Preapproval", FormMethod.Post, new { name = "form1", id = "form1", enctype = "multipart/form-data", @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
 @Html.ValidationSummary(false, "", new { @class = "field-validation-error text-danger" })
 //template to generate dynamic rows  
<table id="Newservice" style="display:none">
 <tr>
<td><select  style="width:100px" class="code_type" name="provider_service_dtls[#].activity_code_type" value></select> </td>
<td><select  style="width:100px" class="act_type" name="provider_service_dtls[#].activity_type" value></select> </td>
<td><input  class="act_name" style="width:150px" type="text" name="provider_service_dtls[#].activity_name"  value /></td>
<td><input  class="clini" style="width:150px" type="text" name="provider_service_dtls[#].clinician" value /></td>
<td><input  class="" style="width:40px" type="text" name="provider_service_dtls[#].unit_amt" value /></td>
<td><input  class="" style="width:25px" type="text" name="provider_service_dtls[#].quantity" value="1"/></td>
<td><input  class="" style="width:40px" type="text" name="provider_service_dtls[#].net_amt" readonly value="10" />
<input type="hidden" name="provider_service_dtls.Index" value="%" />
</td>
<td><input id="delete" class="delete" value="X" type="button"></td>
</tr>
</table>
//template ends here
<table id="service" class="table table-striped table-hover table-bordered">
 <tbody>
@if (Model != null)
{
for (int i = 0; i < Model.provider_service_dtls.Count; i++)
{
<tr>
 <td> @Html.DropDownListFor(m => m.provider_service_dtls[i].activity_code_type, (SelectList)@ViewBag.activity_code_type, "--- Select Activity Code Type ---", new { @class = "m-wrap" })</td>
 <td> @Html.DropDownListFor(m => m.provider_service_dtls[i].activity_type, (SelectList)@ViewBag.activity_type, "--- Select Activity Type ---", new { @class = "m-wrap" })</td>
 <td>@Html.TextBoxFor(m => m.provider_service_dtls[i].activity_name)</td>                                                
<td>@Html.TextBoxFor(m => m.provider_service_dtls[i].clinician)</td>
 <td>@Html.TextBoxFor(m => m.provider_service_dtls[i].unit_amt)</td>
 <td>@Html.TextBoxFor(m => m.provider_service_dtls[i].quantity)</td>
 <td>@Html.TextBoxFor(m => m.provider_service_dtls[i].net_amt)
<input type="hidden" name="provider_service_dtls.Index" value="@i" />
 </td>
<td><input id="delete" class="delete" value="X" type="button"></td>
 </tr>
 }
 }
 </tbody>
 <tbody>
//row for showing total of quantity and net_amt
<tr id="totals">
<td></td>
<td></td>
 <td></td>
 <td></td>
<td>Total</td>
<td style="text-align:right">0</td>
 <td style="text-align:right">0</td>
<td></td>
 </tr>
 </tbody>
</table>
 }
填写表单后单击“提交”,然后单击“提交”,提交成功,并将值传递给provider_service_dtls,如下所示

enter image description here

现在我添加了一个jquery方法,通过乘以数量和统一量来查找total和net_amt

Jquery

var body = $('#service').children('tbody').first();
var totals = $('#totals');
 body.on('change', 'input[type="text"]', function () {
var cells = $(this).closest('tr').children('td');
var value1 = cells.eq(4).find('input').val();
var value2 = cells.eq(5).find('input').val();
var value3 = new Number(value1) * new Number(value2)
cells.eq(6).find('input').val(value3.toFixed(2));
var total = 0;
var i = 0;
// get the column
var columnIndex = $(this).closest('td').index();
 var rows = body.find('tr');
 $.each(rows, function () {
var amount = $(this).children('td').eq(columnIndex).children('input[type="text"]').val();
total += new Number(amount);
     });
totals.children('td').eq(columnIndex).text(total);
    });

添加此Jquery后,totalnet_amtservice table columns发生更改但provider_service_dtls以空值回发时成功计算,如下所示< / p>

enter image description here

现在我发现了这个问题 这条线

cells.eq(6).find('input').val(value3.toFixed(2)); 

正在制作这个..如果我对这一行发表评论并提交provider_service_dtls,那么在发回时会有值{/ p>

我试过

cells.eq(6).text(new Number(value1) * new Number(value2));

此处也值不回发

我不明白为什么在jquery中存在此行时,它不会将provider_service_dtls的值返回给控制器。

请帮忙解决这个问题

1 个答案:

答案 0 :(得分:1)

我发现了问题

cells.eq(6).find('input').val(value3.toFixed(2)); 

脚本中的这一行会将单元格(6)中类型'input'的值更改为value3的值。

在单元格6内有两种输入类型1.input type text for net_amt2. input type hidden i for the index

因此上面的jquerys tatement会将输入类型值都更改为value3。因此,索引将与动态创建的行的索引不匹配。发布时,它无法识别要使用索引值回发的行。

所以我将脚本更改为

cells.eq(6).find('input[type="text"]').val(value3.toFixed(2));

所以这只会改变net_amt的输入类型文本的值。输入类型隐藏i的值将保持不变。

感谢@jasen的评论。 inspect元素有助于发现此问题。