2

我正在开发一个 MVC5 应用程序。主屏幕上有一个网格,允许用户查看数据并转移到多个视图,以对每条记录执行各种操作。其中之一是[编辑]。

我遇到的问题如下:由于数据量大,可以方便地过滤数据(比如特定位置),然后从那里编辑记录。此网格上的过滤器(来自 CodePlex 的 Grid.MVC)通过修改 URL(http://homeURL/?grid-filter=Location.DEPT__1__accounting)部分执行过滤,例如 1 是 Equals,2 是 Cotains,3 是 StartsWith,以及4 是 EndsWith,然后在接下来的 2 个下划线之后是搜索条件。

这个功能很好,但是在从编辑返回 [POST] 时,用户当前返回到主索引视图,而没有设置过滤条件(迫使他们一遍又一遍地进入并添加过滤条件,然后再对记录执行类似的编辑相同的标准)。

我的 POST-EDIT 方法当前设置为包括:

        if (ModelState.IsValid)
        {
            collection.MODIFIED_DATE = DateTime.Now;
            collection.MODIFIED_BY = System.Environment.UserName;

            db.Entry(collection).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index", "Home");
        }

对于我的尝试,我首先考虑返回带有更新集合的视图return View(collection)(我考虑在数据库中添加一个字段,比如 LAST_FILTERED_URL,但这感觉就像一个过度生长的创可贴。

有谁知道一个干净的方法来解决这个问题?


编辑

我早些时候曾想过做一些类似于 Andrea 的建议的事情,但没有想过使用重定向中传递的 url-filter 的参数进行显式重定向。以下是我当前的代码GET/POST Edit

    // GET: ENITTY_Collection/Edit/5
    public async Task<ActionResult> Edit(int id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ENTITY_COLLECTION entity_Collection = await db.ENTITY_COLLECTION.FindAsync(id);
        if (entity_Collection == null)
        {
            return HttpNotFound();
        }

        // Other code for Controls on the View

        return View(entity_Collection);
    }

        // POST: ENTITY_Collection/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Id,One_Id,Two_Id,Three_Id,Four_Id,Five_Id,Six_Id,field7,field8,field9,...field18,created_date,created_by,modified_date,modified_by")] ENTITY_COLLECTION entity_Collection)
        {
            if (ModelState.IsValid)
            {
                entity_Collection.MODIFIED_DATE = DateTime.Now;
                entity_Collection.MODIFIED_BY = System.Environment.UserName;

                db.Entry(entity_Collection).State = EntityState.Modified;
                await db.SaveChangesAsync();
                //return RedirectToAction("Index", "Home");
                return View(entity_Collection);
            }

            // Other code for if Model is Invalid before returning to View.

            return View(entity_Collection);
        }

GET-Edit View我喜欢Andrea的建议,但我仍然需要一种好方法来存储用户第一次导航POST-Edit到保存。

有什么想法吗?

4

3 回答 3

1

我不确定这是否是我所追求的最正确的方法,但似乎对我有用的是Session使用价值。

在我的GET方法中,我存储了 URL:

Session["returnURL"] = Request.UrlReferrer.AbsoluteUri;

然后在我保存对记录的更改后POST使用这个值:Redirect()

var returnURL = (Session["returnURL"] != null) ? Session["returnURL"].ToString() : Url.Action("Index", "Home");
return Redirect(returnURL);

到目前为止,所有初始测试都导致返回到主视图,其中所有排序/过滤标准在输入记录以进行更新之前就位。

于 2015-06-10T13:38:34.837 回答
0

您是否尝试过更改通过当前过滤器以重定向到操作,如下所示?注意:我假设:

  1. 您正在重定向到同一个控制器
  2. 你有一个名为currentFilterValue的控制器参数

    RedirectToAction("Index", "Home",new { grid-filter = currentFilterValue });

于 2015-06-09T12:07:37.120 回答
0

Microsoft 的 MVC 项目的默认 LoginController 包括一组使用 returnUrl 参数的方法。使用这种做法,您可以在打开编辑器时包含一个返回 URL,当编辑完成时,将用户返回到过滤器完好无损的前一个屏幕(假设它们在 URL 中)。

我的视图模型基类有一个用于存储 ReturnURL 的属性,如果设置了该属性,则将其存储为隐藏。

@Html.HiddenFor(model => model.ReturnUrl)

我从编辑中发布的操作具有以下逻辑:

if (viewModel.ReturnUrl.IsNotNullOrEmptyTrimmed())
{
    return RedirectToLocal(viewModel.ReturnUrl, "ActionName", "ControllerName");
}
else
{
    // Default hard coded redirect
}

为了防止某些注入,您将需要一个像这样的验证方法(上面调用)来确保 URL 有效:

protected ActionResult RedirectToLocal(string returnUrl, string defaultAction, string defaultController)
{
    try
    {
        if (returnUrl.IsNullOrEmptyTrimmed())
            return RedirectToAction(defaultAction, defaultController);

        if (Url.IsLocalUrl(returnUrl))
            return Redirect(returnUrl);

        Uri returnUri = new Uri(returnUrl);
        if (Url.IsLocalUrl(returnUri.AbsolutePath))
        {
            return Redirect(returnUrl);
        }
    }
    catch
    {
    }

    return RedirectToAction(defaultAction, defaultController);
}

希望这能给你一些想法并走上正确的道路。

于 2015-06-10T03:17:06.323 回答