MVC 5动作结果缓存提交

时间:2014-11-18 16:26:28

标签: asp.net-mvc asp.net-mvc-5

我有一个项目列表

List 当我点击动作/编辑它弹出一个jQuery对话

edit

JQuery的

  $(".atarieditbtn").click(function () {
        var id = $(this).data('id');
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
        $.ajax({
            url: "/Client/EditATRI/" + id,
            cache: false,
            type: "GET",
        }).done(function (result) {
            $("#atari-detail-modal").html(result);
            $("#atari-detail-modal").dialog({
                title: "Edit ATARI", height: 400, width: 500, dialogClass: "no-close",
                buttons: [
                  {
                      text: "Save changes",
                      click: function (e) {
                          var bValid = true;
                          bValid = bValid && checkValue($('#Allocation'), "Allocation");
                          bValid = bValid && checkValue($('#Term'), "Term");
                          if (bValid) {
                              var eform = $("#frmATARIEdit");
                              var d = $(this);
                              var postData = eform.serializeArray();
                              var formURL = eform.attr("action");
                              $.ajax(
                              {
                                  url: formURL,
                                  type: "POST",
                                  data: postData,
                                  success: function (data, textStatus, jqXHR) {
                                      $.cookie('tab', '4');
                                      d.dialog("close");
                                      $("#ClientDIV").load("/Client/EditClientInfo?id=" + $('#id').val());                                     
                                  },
                                  error: function (jqXHR, textStatus, errorThrown) { }
                              });

                          }
                          else {
                              if (e.preventDefault) {
                                  e.preventDefault();
                              } else {
                                  e.returnValue = false;
                              }
                          }
                      }
                  },
                  {
                      text: "Close",
                      click: function (e) {
                          if (e.preventDefault) {
                              e.preventDefault();
                          } else {
                              e.returnValue = false;
                          }
                          $(this).dialog("close");
                      }
                  }
                ]
            });
        });
    });

加载列表后首先编辑工作,第二行将显示数据,但提交时会给出最后编辑的对象(给ActionResult编辑(PMP_ATRIs oATRI))

控制器

  public ActionResult Edit(int id)
    {

        PMP_ATRIs oATRI = db.PMP_ATRIs.Find(id);

        // existing sum of  
        var oList = from oA in db.PMP_ATRIs where (oA.ClientID == oATRI.ClientID && oA.id != oATRI.id) select oA;
        var Total = oList.Sum(o => o.Allocation);
        //ATRI
        var oATARI = from d in db.PMP_ATRIType orderby d.Name select new { id = d.id, name = d.Name };
        ViewBag.ATARI = new SelectList(oATARI, "id", "name", oATRI.ATARI);
        ViewBag.Total = 100 - Total;
        return View(oATRI);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(PMP_ATRIs oATRI)
    {

        if (ModelState.IsValid)
        {

            oATRI.ChangedOn = DateTime.Now;
            oATRI.ChangedBy = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1);
            oATRI.ApprovedOn = null;
            oATRI.ApprovedBy = null;
            db.Entry(oATRI).State = EntityState.Modified;
            db.SaveChanges();
        }

        //var oATARI = from d in db.PMP_ATRIType orderby d.Name select new { id = d.id, name = d.Name };
        //ViewBag.ATARI = new SelectList(oATARI, "id", "name", oATRI.ATARI);

        //return View(oATRI);
        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

查看

 @model PMP_Intranet.Models.PMP_ATRIs
    @{
        Layout = null;
    }
    @using (Html.BeginForm(null, null, FormMethod.Post, new { name = "frmATARIEdit", id = "frmATARIEdit" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.id)
        @Html.HiddenFor(model => model.ClientID)
        @Html.HiddenFor(model => model.Amount)
        @Html.Hidden("Total", null)
        <table class="table table-hover table-condensed">
            <tr>
                <td align="right"><b>ATARI</b></td>
                <td></td>
                <td>
                    @Html.DropDownList("ATARI", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.ATARI, "", new { @class = "text-danger" })
                </td>
            </tr>       
            <tr>
                <td align="right"><b>Allocation</b></td>
                <td></td>
                <td align="right">
                    <div class="input-group">
                        @Html.EditorFor(model => model.Allocation, new { htmlAttributes = new { @class = "form-control", step = "0.01", @min = ".01", @type = "number" } })
                        @Html.ValidationMessageFor(model => model.Allocation, "", new { @class = "text-danger" })
                        <span class="input-group-addon">%</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td align="right"><b>Term</b></td>
                <td></td>
                <td>
                    @Html.EditorFor(model => model.Term, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Term, "", new { @class = "text-danger" })
                </td>
            </tr>
            <tr>
                <td colspan="3"><p class="validateTips"></p></td>
            </tr>
        </table>
    }
    <script>
        $("#Allocation").change(function () {
            if ($(this).val() > $('#Total').val()) {
                updateTips("Allocation cannot be morethan " + $('#Total').val());
            }
            else {
                updateTips("");
            }
        });
    </script>

1 个答案:

答案 0 :(得分:0)

而不是

d.dialog("close");

使用

d.dialog("destroy");

这会杀死对话值