C# 从“编辑部分视图”更新ListIndex部分视图而不刷新页面
我有两个局部视图,一个用于编辑,另一个用于包含网格列表表数据的ListIndex 我想在单击编辑部分视图表单上的“更新”按钮时获得更新的ListIndex页面 此处页面不会自动更新。只有在刷新页面后,才会对其进行更新 下面是ListIndex视图的代码-locationIndex.cshtmlC# 从“编辑部分视图”更新ListIndex部分视图而不刷新页面,c#,ajax,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Ajax,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我有两个局部视图,一个用于编辑,另一个用于包含网格列表表数据的ListIndex 我想在单击编辑部分视图表单上的“更新”按钮时获得更新的ListIndex页面 此处页面不会自动更新。只有在刷新页面后,才会对其进行更新 下面是ListIndex视图的代码-locationIndex.cshtml @MvcHtmlString.Create( @grid.GetHtml( tableStyle: "tablegeneral",
@MvcHtmlString.Create(
@grid.GetHtml(
tableStyle: "tablegeneral",
fillEmptyRows: true,
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[]
{
grid.Column("LOC_NAME","BINNO"),
grid.Column(format: @<text> @Ajax.EditModalDialogActionLink("Edit","Edit","Location","Edit Location","Model",(long)item.LOC_CODE)</text>),
grid.Column(format: @<text> @Ajax.EditModalDialogActionLink("Delete", "Delete","Location", "Delete Location","model",(long)item.LOC_CODE)</text>)
}).ToString().Replace("<tfoot>", "<tfoot> <tr><td colspan=\"7\" align=\"center\"> Records " + firstRecord + " to " + lastRecord + " of " + grid.TotalRowCount + "</tr>"))
这是我的位置控制器:
public class LocationController : MasterController
{
private ROCKWELLTESTEntities db = new ROCKWELLTESTEntities();
//
// GET: /Master/Location/
public ActionResult LocationIndex(string text)
{
if (TempData["msg"] != null)
{
ViewData["error"] = TempData["msg"];
text = null;
}
IEnumerable<location_master> locms;
if (text == null)
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0));
}
else
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0) && l.LOC_NAME.Contains(text));
}
return View(locms);
}
public ActionResult Edit(int id)
{
location_master locms = db.location_master.Find(id);
return PartialView(locms);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(location_master locms)
{
return editProcessDialog(locms, "Location Updated");
}
ActionResult editProcessDialog(location_master locms, string message)
{
if (ModelState.IsValid)
{
if (locms != null)
{
db.Entry(locms).State = EntityState.Modified;
db.SaveChanges();
ControllerContext.HttpContext.Response.Write(string.Format("<div data-dialog-close='true' data-dialog-result='{0}'/>", message));
return PartialView("Edit");
}
else
{
ModelState.AddModelError("", string.Format("Location Not Exist"));
}
}
return PartialView(locms);
}
}
公共类位置控制器:主控制器
{
private RockwellTestenties db=新的RockwellTestenties();
//
//获取:/Master/位置/
公共操作结果位置索引(字符串文本)
{
if(TempData[“msg”]!=null)
{
ViewData[“error”]=TempData[“msg”];
text=null;
}
IEnumerable locms;
if(text==null)
{
locms=db.location_master.ToList(),其中(l=>l.LOC_DEL.Equals(0));
}
其他的
{
locms=db.location_master.ToList()。其中(l=>l.LOC_DEL.Equals(0)和&l.LOC_NAME.Contains(text));
}
返回视图(locms);
}
公共操作结果编辑(int id)
{
location\u master locms=db.location\u master.Find(id);
返回局部视图(locms);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果编辑(位置\主locms)
{
返回editProcessDialog(locms,“位置更新”);
}
ActionResult editProcessDialog(位置\主locms,字符串消息)
{
if(ModelState.IsValid)
{
如果(locms!=null)
{
db.Entry(locms).State=EntityState.Modified;
db.SaveChanges();
ControllerContext.HttpContext.Response.Write(string.Format(“,message));
返回部分视图(“编辑”);
}
其他的
{
ModelState.AddModelError(“,string.Format(“位置不存在”);
}
}
返回局部视图(locms);
}
}
无需刷新整个表。您已经知道表单控件中的值,因此如果保存成功,只需更新相应行中的单元格即可。保存数据成功。但是如何才能只更新该页面中相应行中的那些单元格。当您单击“编辑”链接时,您可以使用var cells=$(this.nexist('tr')。find('td'))获取相关行
然后在成功回调中,您可以使用cells.eq(0).text($('#LOC_NAME').val())更新行单元格代码>等。实际上,您应该只在视图中包含一个表单(隐藏),当您单击“编辑”按钮时,执行相反的操作-即从现有行值填充表单控件并将其显示为对话框。当前使用(过时的)Ajax.ActionLink()
和Ajax.BeginForm()
会降低性能。
public class LocationController : MasterController
{
private ROCKWELLTESTEntities db = new ROCKWELLTESTEntities();
//
// GET: /Master/Location/
public ActionResult LocationIndex(string text)
{
if (TempData["msg"] != null)
{
ViewData["error"] = TempData["msg"];
text = null;
}
IEnumerable<location_master> locms;
if (text == null)
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0));
}
else
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0) && l.LOC_NAME.Contains(text));
}
return View(locms);
}
public ActionResult Edit(int id)
{
location_master locms = db.location_master.Find(id);
return PartialView(locms);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(location_master locms)
{
return editProcessDialog(locms, "Location Updated");
}
ActionResult editProcessDialog(location_master locms, string message)
{
if (ModelState.IsValid)
{
if (locms != null)
{
db.Entry(locms).State = EntityState.Modified;
db.SaveChanges();
ControllerContext.HttpContext.Response.Write(string.Format("<div data-dialog-close='true' data-dialog-result='{0}'/>", message));
return PartialView("Edit");
}
else
{
ModelState.AddModelError("", string.Format("Location Not Exist"));
}
}
return PartialView(locms);
}
}