我在主视图中创建了一个webgrid。我在每一行上放置一个编辑按钮,调用部分视图以显示该记录的可编辑字段。但是,当我运行我的解决方案进行测试时,我将编辑一条记录,然后导航回我的主视图,网格显示更新的数据,但当我再次单击编辑时,它显示的数据最初不是我更新的数据。我发布了我的观点和控制器。我错过了什么?
主视图
@{
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")
}
控制器
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");
}