我正在开发一个 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
到保存。
有什么想法吗?