我有一个显示项目列表的视图,我打算将操作与视图一起使用(视图是相同的,只是显示了不同的数据)
我正在使用一个对话框来添加它(这很好用)我将在整个过程中使用相同的对话框。
我的查询在下面的例子中得到了最好的展示......
设备视图
ActionResult PCs()...
ACtionResult 笔记本电脑()...
所以在 PC 控制器下,我点击编辑并修改数据,当我更新数据时,我返回了......我想返回到我开始的操作
因此,如果我编辑一台电脑,在更新时,对话框会将我返回到电脑,如果我编辑一台笔记本电脑,我会返回笔记本电脑。
我在想我需要将另一个变量传递给 addeditview 以显示哪个控制器引用了我。
以上是否有意义,任何人都可以提供实现这一目标的最佳方法吗?
编辑:添加当前代码
我现在想不出该怎么做,我以为 id 在 users 下设置模式,并在 addit get 中检索模式,但不知道如何将模式发送到 add edit get
我可以在下面的某个地方做吗?
@Html.ActionLink("Edit", "AddEditRecord", new { id = item.ID }, new { @class = "editDialog" })
#####控制器
// 获取:/用户
public ActionResult Users()
{
ViewBag.Title = "User Equipment";
ViewBag.Mode = "Users";
string[] arrItems = new string[] { "Laptop", "Workstation", "Mobile","Monitor","Other Peripheral","Home Printer","Home Router","Removable Device" };
var tblequipments = from d in db.tblEquipments.Include(t => t.User).Include(t => t.ChangeLog).AsEnumerable()
where (arrItems.Contains(d.AssetType)) &&
(d.Deleted != 1 || d.Deleted == null) &&
(d.Stock != 1 || d.Stock == null) &&
(d.DecommissionDate == Convert.ToDateTime("1900-01-01") || d.DecommissionDate == Convert.ToDateTime("0001-01-01") || d.DecommissionDate == null)
select d;
return View("Equipment",tblequipments.ToList());
}
//GET: /AddEdit
[HttpGet]
public ActionResult AddEditRecord(int? id,string mode)
{
ViewBag.Mode = mode;
if (Request.IsAjaxRequest())
{
if (id != null)
{
ViewBag.IsUpdate = true;
tblEquipment Equipment = db.tblEquipments.Where(m => m.ID == id).FirstOrDefault();
return PartialView("_AddEdit", Equipment);
}
ViewBag.IsUpdate = false;
return PartialView("_AddEdit");
}
else
{
if (id != null)
{
ViewBag.IsUpdate = true;
tblEquipment Equipment = db.tblEquipments.Where(m => m.ID == id).FirstOrDefault();
return PartialView("AddEdit", Equipment);
}
ViewBag.IsUpdate = false;
return PartialView("AddEdit");
}
}
[HttpPost]
public ActionResult AddEditRecord(tblEquipment Equipment, string cmd)
{
if (ModelState.IsValid)
{
switch (cmd)
{
case "Add":
try
{
db.tblEquipments.Add(Equipment);
db.SaveChanges();
return RedirectToAction("Index");
}
catch { }
break;
case "Update":
try
{
tblEquipment Item = db.tblEquipments.Where(m => m.ID == Equipment.ID).FirstOrDefault();
if (Item != null)
{
Item.AssetNo = Equipment.AssetNo;
Item.MachineName = Equipment.MachineName;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch { }
break;
}
}
if (Request.IsAjaxRequest())
{
return PartialView("_AddEdit", Equipment);
}
else
{
return View("AddEdit", Equipment);
}
}
添加编辑模式视图
@using (Ajax.BeginForm("AddEditRecord", "Equipment", new AjaxOptions { HttpMethod = "POST",OnSuccess = "onSuccess()", LoadingElementId = "dvLoading" }))
{
@Html.ValidationSummary(true)
<div id="equipmentDialog">
<fieldset>
<legend>Product</legend>
@if (ViewBag.IsUpdate == true)
{
@Html.HiddenFor(model => model.ID)
}
<div class="editor-label">
@Html.LabelFor(model => model.MachineName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MachineName)
@Html.ValidationMessageFor(model => model.MachineName)