Reputation: 1
I am using FormCollection and GetValues() and I am encountering issues if I resubmit the information. I have a button that Calculates the Slope of a range of entries. When I Calculate the Slope I save the information in a Temp Table and redisplay the values in a list. When I first save the entries there are no issues in saving and building the list. However when I try to Calculate the Slope again it will not recognize the previous entries or any changes to the previous entries but any new items will be recognized.
my controller for create displays the List if there are any values in the Temp Table
model.TempPointList = db.Temp_SO2AuditMultipointCal
.Where(x => x.SO2AuditCalID == tempID)
.OrderByDescending(x => x.CalPoint).ToList();
and the HttpPost looks at the FormCollection
if (Command == "CalcSlope")
{
string[] pointItems1 = form.GetValues("Point");
string[] concItems1 = form.GetValues("CalConc");
string[] dasItems1 = form.GetValues("DAS");
string[] diffItems1 = form.GetValues("Diff");
//use arrays to calculate the slope ...
}
my view collects and displays the values
<table class="table">
<tbody id="myTable">
<tr>
<th>Select</th>
<th>Point</th>
<th>Cal Conc</th>
<th>DAS</th>
</tr>
foreach (var item in Model.TempPointList)
{
<tr>
@Html.HiddenFor(modelItem => item.Temp_SO2AuditMultipointCalID)
<td><input type='checkbox' name='record'></td>
<td>@Html.EditorFor(modelItem => item.CalPoint, new { @autocomplete = false })</td>
<td>@Html.EditorFor(modelItem => item.CalConc, new { @autocomplete = false, @Cache = false })</td>
<td>@Html.EditorFor(modelItem => item.DASResponse, new { @autocomplete = false, @Cache = false })</td>
</tr>
}
}
when I add an item to the list I had an Add Row button that executes the following function
$(".add-row").click(function () {
var Point = $("#Point").val();
var CalConc = $("#CalConc").val();
var DAS = $("#DAS").val();
var count = $('#myTable').children('tr').length;
$('#counter').html(count);
$('#myTable').append("<tr><td><input type='checkbox' name='record'></td><td><input type='text' name='Point' value= " + Point + "></td><td><input type='text' name='CalConc' value= " + CalConc + "></td><td><input type='text' name='DAS' value= " + DAS + "></td></tr>");
});
I have tried setting @Cache = false
and adding ModelState.Clear();
to the controller however they do not affect anything.
Upvotes: 0
Views: 83
Reputation: 1
I solved the problem by removing the List and display of the list. I added a window.onload function.
window.onload = function () {
if ($('#Temp').val() == "yes") {
$.ajax({
type: "POST",
url: "/CertsHelper/SOAudit",
data: { id: $('#SO2AuditCalID').val() },
dataType: "json",
success: function (index) {
var count = $('#myTable').children('tr').length;
$('#counter').html(count);
$('#myTable').append(index);
},
error: function (error) {
alert("Display Temp Points not working: " + error);
}
});
}
}
The function executes a ResultAction that provides a string result for the table append.
public JsonResult SOAudit(int id)
{
var tempPoints = db.Temp_SO2AuditMultipointCal
.Where(x => x.SO2AuditCalID == id);
string Color = "";
StringBuilder sb = new StringBuilder();
foreach (var x in tempPoints)
{
sb.Append("<tr><td><input type='checkbox' name='record'></td><td><input type='text' name='Point' value= " + x.CalPoint + "></td><td><input type='text' name='CalConc' value= " + x.CalConc + "></td><td><input type='text' name='DAS' value= " + x.DASResponse + "></td></tr>");
//delete the recorded temp point
Temp_SO2AuditMultipointCal _Points = db.Temp_SO2AuditMultipointCal.Find(x.Temp_SO2AuditMultipointCalID);
db.Entry(_Points).State = EntityState.Deleted;
db.SaveChanges();
}
string points = sb.ToString();
return Json(points);
}
Upvotes: 0