返回到倒数第二个网址的MVC(与previous过滤条件返回查看应用)? [英] Return to second to last URL in MVC (return View with previous filter conditions applied)?

查看:169
本文介绍了返回到倒数第二个网址的MVC(与previous过滤条件返回查看应用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC5应用。在主屏幕上是一个网格,允许用户查看数据,并传送到多个视图在每个记录中的各种动作。其中之一是一个。

I'm working on an MVC5 application. On the home screen is a grid allowing users to view Data and be transferred to a number of Views for various actions on each record. One of these is an .

我遇到的问题是:由于数据可以很方便地过滤数据下(比如说到特定的位置),然后编辑记录从那里量。在这个网格(Grid.MVC从codePLEX)过滤器进行修改的URL( HTTP过滤部分:// homeURL /网格滤波器= Location.DEPT__1__accounting 的),例如1存在等于,2为Cotains,3为StartsWith和4被的endsWith再下2下划线作为搜索标准之后。

The issue I'm encountering is as follows: due to the amount of data it is convenient to Filter the data down (say to a specific location) and then Edit records from there. The filter on this grid (Grid.MVC from CodePlex) performs filtering partially by modifying the URL (http://homeURL/?grid-filter=Location.DEPT__1__accounting) such as 1 being Equals, 2 being Cotains, 3 being StartsWith, and 4 being EndsWith and then after the next 2 underscores being the search criteria.

这个功能正常,但是在从编辑[POST]返回当前被返回到主索引视图,而不仍设置筛选条件的用户(迫使他们一遍又一遍地去并进行同样的编辑之前添加过滤标准的相同的标准的记录)。

This functions fine, however upon [POST] return from the Edit the user currently is returned to main Index view without the filtering criteria still set (forcing them to go in over and over and add filtering criteria before performing the similar EDIT on records of the same criteria).

我的POST-EDIT方法是目前设定为包括:

My POST-EDIT method is currently setup to include:

        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");
        }

有关我尝试我首先想到用更新的集合返回查看(返回查看(集合)),但这当然只是把我带回到编辑视图,不与过滤跌$ p $数据网格主视图pviously指定。我认为在数据库中,像LAST_FILTERED_URL东西添加字段,但是这感觉就像一个杂草丛生的创可贴。

For my attempts I had first thought to return the View with the updated collection (return View(collection)) but this of course just takes me back to the EDIT view, not the home view with the data grid filtered down as previously specified. I considered adding a field in the database, something like LAST_FILTERED_URL, but this just feels like an overgrown band-aid.

有谁知道一个干净的方式去了解呢?

Does anyone know of a clean way to go about this?

修改

我想早做上类似于安德烈的建议什么的,但没想到做与重定向通过URL过滤器的参数明确的重定向。下面是我目前的code为 GET / POST编辑

I had thought to do something similar to Andrea's suggestion early on, but had not thought of doing an explicit redirect with the Parameter of the url-filter passed in the Redirect. Below is my current code for the 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);
        }

我喜欢安德烈的建议,但我仍然需要一个很好的方式来存储用户的URL时,他们首先浏览到 GET-编辑视图,然后使用过滤网址的价值回报用户到previous位置和放大器;当后修改完成和改变保存过滤选项。

I like Andrea's suggestion, but I still need a good way to store the URL the user has when they first navigate to the GET-Edit View, and then use that filtered URL value to return the user to that previous location & filter option when the POST-Edit completes and changes have saved.

有什么想法?

推荐答案

我不知道这是否是要去什么我之后,但什么似乎是为我工作是使用最正确的方法一个会话值。

I'm not sure if this is the most correct way of going about what I'm after, but what appears to be working for me is the use of a Session value.

在我的 GET 方法我保存的网址:

In my GET method I store the URL:

会话[RETURNURL] = Request.UrlReferrer.AbsoluteUri;

然后在我的 POST 我使用该值的重定向()将更改保存到记录后:

Then in my POST I use this value in a Redirect() after saving changes to the record:

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

到目前为止,所有初步测试是导致回报与全部到位排序/过滤标准的主要看法是进入前的战绩进入了更新。

So far all initial testing is resulting in a return to the main view with all sorting/filtering criteria in place before the record was entered into for update.

这篇关于返回到倒数第二个网址的MVC(与previous过滤条件返回查看应用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆