使用多个控制器操作的视图,返回初始视图

时间:2013-07-18 13:28:34

标签: asp.net-mvc

我有一个显示项目列表的视图,我打算在视图中使用动作(视图相同,只显示不同的数据)

我正在使用一个对话框来执行addit(这很好用)我将在整个过程中使用相同的对话框。

我的查询最好在以下示例中进行演示......

设备视图

ActionResult个人电脑()...... ACtionResult笔记本电脑()......

所以在PC控制器下我点击编辑并修改数据,当我更新数据时,我回来了...我想回到我开始的动作

所以,如果我编辑一台电脑,在更新时,对话框会将我返回到电脑,如果我编辑笔记本电脑,我会回到笔记本电脑。

我在想我需要将另一个变量传递给addeditview以显示控制器对我的评价。

以上是否有意义,有人能提供实现这一目标的最佳方法吗?

由于

编辑:添加当前代码

我觉得现在该怎么办,我认为id在用户下设置模式,并检索模式,在addit get中,但我不确定如何将模式发送到add edit get

我可以在下面的某个地方做到吗? 感谢

@Html.ActionLink("Edit", "AddEditRecord", new { id = item.ID }, new { @class = "editDialog" })
调节器

// GET:/ Users

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)

1 个答案:

答案 0 :(得分:0)

排序!

1.我添加了一个viewbag动作 2.在查询字符串中使用该视图包进行编辑 3.在隐藏字段中输入查询字符串 4.在表格帖子上签署字段值并重定向到字段值的操作

感谢您的帮助