0

I created a webgrid in my main view. I put an Edit button on each row that will call a partial view to show editable fields for that record. But when I run my solution to test I will edit a record then navigate back to my main view and the grid shows the updated data but when I click Edit again it shows the data it originally was not the data I updated with. I posted my views and controllers. What am I missing?

Main View

@{
ViewBag.Title = "Project";
}


<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>

<h2>Project</h2>

<div id="test">
@{
var grid = new WebGrid(Model);
}
@grid.GetHtml(tableStyle: "grid",
selectedRowStyle: "selected",
columns: grid.Columns(
grid.Column("Edit", format: @<text><input id="button" type="submit" value="Edit" onclick="myFunction(@item.ProjectId)"></text>),
grid.Column("ProjectName", "Project"),
grid.Column("Address", "Address"),
grid.Column("City", "City"),
grid.Column("State", "State"),
grid.Column("Zip", "Zip")))
</div>

<div id="projectDetails" style="text-align:center">
</div>

<script>
function success(data) {
$("#projectDetails").html(data);
myFunction();
}

function myFunction(id) {
$.ajax({
url: '/Setup/ProjectEdit',
type: 'GET',
data: { projectid: id },
datatype: 'html',
success: function (data) {
success(data);
}
});

function success(data) {
$('#projectDetails').html(data);
}
}

</script>

Partial View

@model FS2.Models.Project


<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>

@using (Ajax.BeginForm("ProjectEdit", "Setup", new AjaxOptions { UpdateTargetId = "result" }))
//@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Project</h4>
<div id="result"></div>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ProjectId)

<div class="form-group">
@Html.LabelFor(model => model.ProjectName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectName)
@Html.ValidationMessageFor(model => model.ProjectName)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Address, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.City, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.State, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.State)
@Html.ValidationMessageFor(model => model.State)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Zip, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Zip)
@Html.ValidationMessageFor(model => model.Zip)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Phone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone)
@Html.ValidationMessageFor(model => model.Phone)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.UpdatedBy, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UpdatedBy)
@Html.ValidationMessageFor(model => model.UpdatedBy)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.UpdatedDt, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UpdatedDt)
@Html.ValidationMessageFor(model => model.UpdatedDt)
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

Controllers

public ActionResult ProjectCreate()
{
return View(new ProjectModel());
}

[HttpPost]
public ActionResult ProjectCreate(ProjectModel pjmodel)
{

try
{
Project pj = new Project();

pj.ProjectName = pjmodel.ProjectName;
pj.Address = pjmodel.Address;
pj.City = pjmodel.City;
// pj.State = pjmodel.State;
pj.Zip = pjmodel.Zip;

_db.Projects.Add(pj);
_db.SaveChanges();

return RedirectToAction("Index");
}

catch
{
return RedirectToAction("Index");
}
}

public ActionResult ProjectEdit(int? projectid = null)
{
var model = _db.Projects.Where(e => e.ProjectId == projectid).Single();
return PartialView(model);
}

[HttpPost]
public ActionResult ProjectEdit(int projectid, FormCollection collection)
{
Project coll = _db.Projects.Where(e => e.ProjectId == projectid).Select(e => e).Single();
UpdateModel(coll);
_db.SaveChanges();
return Content("Success", "text/html");
}
Nate
  • 1
  • 2
  • Have you verified the updates are saved to the DB? – Paul Abbott Nov 18 '14 at 21:05
  • Yes I have. When I got back to the main view. The Webgrid shows the updated data but if I click on my edit button for that record it will show the original data from before my update. – Nate Nov 18 '14 at 21:29
  • I mean, have you looked _inside the database_ to verify the record was updated? – Paul Abbott Nov 18 '14 at 21:42
  • Yes in the database it was updated. – Nate Nov 18 '14 at 21:48
  • It's hard to tell, but EF caches values it retrieves and sharing your context `_db` as a global variable (or whatever it is) could be causing the problem. See http://stackoverflow.com/questions/16540572/entity-framework-gives-me-the-old-data-although-i-changed-them and http://stackoverflow.com/questions/824330/should-entity-framework-context-be-put-into-using-statement?rq=1 – Paul Abbott Nov 18 '14 at 22:09
  • I guess my question is I did add a _db.dispose() to my code in my ProjectEdit controllers but it did not help. Those articles are helpful but does not tell me where I need to implement that code. – Nate Nov 19 '14 at 17:09
  • I have found something interesting that could be causing the problem. When I first go to the main view and click on edit to one of the records it fires the ProjectEdit ActionResult. After I hit Save, go back to the main view, click Edit on that same record it does not fire that ProjectEdit ActionResult again even though it does fire the myFunction() javascript function. How can I force it to fire that ActionResult again? – Nate Nov 20 '14 at 14:26
  • http://stackoverflow.com/questions/10011780/prevent-caching-in-asp-net-mvc-for-specific-actions-using-an-attribute ? – Paul Abbott Nov 20 '14 at 16:14
  • Thank you I just found that answer myself. Adding this cache: false in the ajax call fixed this issue. Thank you for the help. – Nate Nov 20 '14 at 16:34

0 Answers0