我有顯示的項目列表的視圖,我打算使用操作與視圖(被顯示視圖一樣的,只是不同的數據)使用多個控制器操作的視圖,返回到初始視圖
我正在使用對話框來執行addedit(這工作正常),我將始終使用相同的對話框。
我的查詢最好在下面的例子演示...
設備查看
的ActionResult電腦()... 的ActionResult筆記本電腦()...
所以下的電腦控制器我打編輯和修改數據,當我更新數據,即時返回......我想返回到我開始與
所以,如果我編輯的電腦操作,在更新的對話框返回我回到電腦,如果我編輯筆記本電腦我回到筆記本電腦。
我想我需要通過傳遞另外一個變量的addeditview展現至極控制器審閱了我。
沒有上述意義,任何人都可以提供實現這一目標的最佳方式是什麼?
感謝
編輯:添加當前代碼
我不能想怎麼做它的那一刻,我以爲ID設置模式下用戶和檢索方式,在addedit得到,但我不知道如何發送模式,以附加編輯獲得
我能做到這一點在下面的地方? 感謝
@Html.ActionLink("Edit", "AddEditRecord", new { id = item.ID }, new { @class = "editDialog" })
控制器
// GET:/用戶
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);
}
}
ADD編輯模式的看法
@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)
我不喜歡這樣的策略,但它是一個個人的選擇。如果您想爲您工作,則必須向視圖發送必須接收重定向的操作的名稱。該觀點正在發佈到另一個行動,對吧? –
是啊所以ActionPC> EquipmentView> EditEquipmentView>更新>回到ActionPC,如果這是有道理的? – AlexW
'EditEquipment'發佈到'Update'。當你這樣做時,你必須告訴該動作將結果重定向到哪裏。因此,您必須從您的表單發送它,並且該值來自原始操作(在這種情況下爲'PC')。這對你有意義嗎? –