MVC模型将多行插入表中而不是仅插入一行

时间:2014-09-05 07:19:25

标签: c# ajax asp.net-mvc

我有这个问题,我创建了一个带有三个下拉列表的模型,我使用AJAX将行插入表中。但似乎正在发生的事情是该行被插入三次。我假设它与我建立模型的方式有关,因为我有三个下拉列表。我无法理解为什么会这样做。任何人都可以解释我哪里出错了。

我的模特是:

[Table("tblDamageLine")]
public class tblDamageLine
{

    [Key]
    public int auditDamageLine_ID { get; set; }
    public int auditId { get; set; }
    public int? zoneId { get; set; }         
    [ForeignKey("zoneId")]
    public virtual tblZoneList tblZoneList { get; set; }

    public int? bodypartId { get; set; }
    [ForeignKey("bodypartId")]
    public virtual tblBodyPartList tblBodyPartList { get; set; }

    public int? damageTypeId { get; set; }
    [ForeignKey("damageTypeId")]
    public virtual tblDamageTypeList tblDamageTypeList { get; set; }

    public int imageId { get; set; }
    [StringLength(100)]
    public string imageFileName { get; set; } // filename to be built using audit ID and date followed by image name
    [StringLength(2000)]
    public string additionalDetails { get; set; }
}

和处理AJAX插入的Controller是:

[HttpPost]
public void ProcessDamageLine(tblDamageLine DamageLine)
{
    if (ModelState.IsValid)
    {
        db.tblDamageLines.Add(DamageLine);
        db.SaveChanges();

    }

}

我的Razor代码是:

<table>
    <tr>
        <td>@Html.DropDownList("zoneId", String.Empty) 
        @Html.ValidationMessageFor(model => model.zoneId)</td>
        <td>@Html.DropDownList("bodypartId", String.Empty)
        @Html.ValidationMessageFor(model => model.bodypartId)</td>
        <td>@Html.DropDownList("damageTypeId", String.Empty)
        @Html.ValidationMessageFor(model => model.damageTypeId)</td>
        <td>@Html.EditorFor(model => model.additionalDetails)
        @Html.ValidationMessageFor(model => model.additionalDetails)</td>
    </tr>
 </table>

Ajax代码是:

 @{
      AjaxOptions options = new AjaxOptions();
      options.HttpMethod = "POST";
      options.OnBegin = "OnBegin";
      options.OnComplete = "OnComplete";
      options.OnFailure = "OnFailure";
      options.OnSuccess = "OnSuccess";
      options.LoadingElementId = "divProgress";
      options.LoadingElementDuration = 1000;
      options.UpdateTargetId = "divResponse";
      options.InsertionMode = InsertionMode.InsertAfter;
 }

编辑 - 以下是tblDamageLines与之关联的模型:

            [Table("tblAuditRecord")]
    public class tblAuditRecord
    {

        [Key]
        public int auditId { get; set; }

        [Column(TypeName = "datetime2")]
        public DateTime dateOfAudit { get; set; }

        [StringLength(50)]
        public string fleetNumber { get; set; }

        [StringLength(50)]
        public string nonFleetNumber { get; set; }

        //public string auditType { get; set; }  check about this one
        [StringLength(200)]
        public string bodyShopName { get; set; }

        [StringLength(20)]
        public string vehicleReg { get; set; }

        [StringLength(30)]
        public string vehicleMake { get; set; }

        [StringLength(50)]
        public string vehicleModel { get; set; }

        [StringLength(150)]
        public string vehicleAdditionalDetail { get; set; }

        public int numberOfDoors { get; set; }

        [StringLength(20)]
        public string vehicleMileage { get; set; }

        public int auditorID { get; set; }

        public string auditorSignature { get; set; }

        [Column(TypeName = "datetime2")]
        public DateTime dateOfSignature { get; set; }

        public bool machinePolished { get; set; }

        public bool vehicleDriven { get; set; }

        public string bodyShopSignature { get; set; }

        [Column(TypeName = "datetime2")]
        public DateTime dateOfBodyShopSignature { get; set; }

        public string additionalInformation { get; set; }

        public virtual ICollection<tblDamageLine> tblDamageLines { get; set; }

    }

非常感谢任何建议,谢谢

编辑完整的剃刀表格

     @using (Ajax.BeginForm("ProcessDamageLine", "Audit", options))
     {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
          <legend>tblDamageLine</legend>

           <div class="editor-field">
              @Html.HiddenFor(model => model.auditId, new { @Value = ViewBag.AuditID})
           </div>
           <table style="width:100%;">
            <tr>
            <th>@Html.Label("Zone")</th>
            <th>@Html.Label("Body Part")</th>
            <th>@Html.Label("Damage Type")</th>
            <th>@Html.Label("Additional Details")</th>
           </tr>
            <tr>
             <td>@Html.DropDownList("zoneId", String.Empty)                                     @Html.ValidationMessageFor(model => model.zoneId)</td>
            <td>@Html.DropDownList("bodypartId", String.Empty)
            @Html.ValidationMessageFor(model => model.bodypartId)</td>
            <td>@Html.DropDownList("damageTypeId", String.Empty)
            @Html.ValidationMessageFor(model => model.damageTypeId)</td>
            <td>@Html.EditorFor(model => model.additionalDetails)
            @Html.ValidationMessageFor(model => model.additionalDetails)</td>
        </tr>
    </table>
    <div class="editor-field">
        @Html.HiddenFor(model => model.imageId)
        @Html.ValidationMessageFor(model => model.imageId)
    </div>


    <p>
        <input type="submit" value="Add" />
    </p>
    <p>
        <input type="button" value="Add Image" onclick="showImageDiv();" />
    </p>
</fieldset>
}

更新:进一步测试后,似乎是Ajax导致了问题。如果我做一个标准的插入它工作,只插入一行。如果我使用Ajax,它会插入3个三行

0 个答案:

没有答案