当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - MVC 5 action-result caches on submit

问题描述:

i have a list of items

when i click action /Edit it popups a jQuery dialogue

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");

}

}

]

});

});

});

first edit after loading the list works, the second line will display the data but when submitting it gives the last edited object ( to ActionResult Edit(PMP_ATRIs oATRI) )

Controller

 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);

}

View

 @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>

网友答案:

instead of

d.dialog("close");

use

d.dialog("destroy");

this kills the dialogue values

分享给朋友:
您可能感兴趣的文章:
随机阅读: