主子表中,操作子表的时候,编辑的需要2个参数 ,一个是主表ZID,一个是自己的ID,这2个参数非常重要,存放在那里好呢,
在AddDto中,不宜用跟主表对应的真实列名,因为那样,映射会出错, 例如子表在Entity中,映射主表的ID是 public virtual CEDLEntity CEDL { get; set; } ,在AddDto 中肯定是用int 类型,便于提取,用一样的名字,映射的时候容易出错, 可以单独取一个属性 ,例如 ZCEDL
我们再回到以前的话题,编辑的时候,需要2个参数,放在Session 牺牲了性能, 不稳定性,这个时候,应该巧妙的放在浏览器参数当中,这样子稳定,相于于静态变量。怎么放在浏览器参数呢,有2种方式
一个是在控制器当中 ,执行控制器方法的时候,视图出来的结果浏览器上肯定带有参数,如下
[HttpPost]
[IgnoreRightFilter]//忽略权限验证
public ActionResult Edit(CEDMcgAddDto dto)
{
if (ModelState.IsValid)
{
var result = _CEDMcgService.Update(dto);
if (result)
return RedirectToAction("Index", new { id = dto.CEDLsonSid });
}
return View(dto);
}
还有一个JS办法如下 在IE上有一个问题id的参数 ,
function editModel() {//编辑
var row = JucheapGrid.GetData();
if (row != null) {
$("#btnEdit").button("loading");
var id = '@ViewContext.RouteData.Values["id"]';//获取路由参数值:
window.location.href = "@Url.Action("Edit")/" + row.Id + "?id=" + id;
} else {
parent.layer.alert("请选择要编辑的数据");
}
}
当然主表的ID也可以在表里进行获取,( 假如表隐藏列中也包含主表ID的话)如下
function editModel() {//编辑
var row = JucheapGrid.GetData();
if (row != null) {
$("#btnEdit").button("loading");
var id = '@ViewContext.RouteData.Values["id"]';//获取路由参数值:
window.location.href = "@Url.Action("Edit")/" + row.Id + "?id=" +row.zid;
} else {
parent.layer.alert("请选择要编辑的数据");
}
}
这个时候就要求提交的时候,提交到列表的时候,IE上也必须有主表ID的参数,如下
/// <returns></returns>
[HttpPost]
[IgnoreRightFilter]//忽略权限验证
public ActionResult Edit(CEDMcgAddDto dto)
{
if (ModelState.IsValid)
{
var result = _CEDMcgService.Update(dto);
if (result)
return RedirectToAction("Index", new { id = dto.CEDLsonSid });
}
return View(dto);
}
假如用JS提交的话如下
$.ajax({
url: "/CEDLson/Edit",
type: "post",
data: jsondata,
contentType: "application/json",
dataType: "json",
success: function (e) {
if (e.flag) {
parent.layer.msg("保存成功");
window.location.href = "@Url.Action("Index/"+Request["ID"])";
}
else {
parent.layer.msg(e.msg);
}
}
});
对,获取浏览器参数 用 Request["ID"] ,非常重要