如何在MVC 4基于用户过滤后的结果 [英] How to Filter result in MVC 4 based on user

查看:100
本文介绍了如何在MVC 4基于用户过滤后的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义authenticantion,当用户登录时,我一直在会话/缓存的必要信息...

I have a custom authenticantion, when user logs in, I keep the necessary information on Session/Cache...

所以,我有一些看法与下拉菜单必须显示按用户ID筛选的数据...
我倒是喜欢知道什么最好的方式来这一结果过滤...

So, I have some Views with DropDowns that must show data filtered by User id... I´d like to known what the best way to filter that result...

1 - 直接在控制器

1 - Direct on Controller?

...   
Model.MyList = repository.GetAll().Where(x => x.User.Id == userId);
return View(Model);

2 - 创建一个动作过滤器(我怎么能做到这一点,而不从数据库查询不必要的数据)

2 - Creating an action filter (How can I do that without querying unnecessary data from DB)

3 - 其他方式

1的问题是,我有一个具有相同的下拉一些意见,所以我将不得不重复同样的code。

The problem with 1 is that I have several views that have the same dropdown, so I will have to repeat the same code.

推荐答案

功能

private void userInfo(ResultExecutingContext filtercontext)
{                                        
    if (filtercontext.Controller.TempData[userId.ToString()] == null)
        filtercontext.Controller.ViewBag.userId =
            filtercontext.Controller.TempData[userId.ToString()] = 
            repository.GetAll().Where(x => x.Id == userId);

    else      //This will load the data from TempData. So, no need to 
              //hit DataBase.
        filtercontext.Controller.ViewBag.userId =
            filtercontext.Controller.TempData[userId.ToString()];

    TempData.Keep();  // This will save your Database hit.
}

过滤法

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filtercontext)
    {
        //Call the Action Method before executing the View and after 
        //executing the Action.
        userInfo(filtercontext);
        base.OnResultExecuting(filtercontext);
    }
}

控制器的操作方法

[MyActionFilter] 
//Whenever Action Method will execute. We will check TempData contains 
//Data or not.
public ActionResult Index()
{
    return View();
}

关于关键点的TempData TempData.Keep()

Key point about TempData and TempData.Keep()


  1. 的TempData 的项目将只标记为删除已阅读了。

  2. 的TempData 项目可以通过调用无标记 TempData.Keep(键)

  3. RedirectResult RedirectToRouteResult 总是调用 TempData.Keep()保留在的TempData

  1. Items in TempData will only tagged for deletion after they have read.
  2. Items in TempData can be untagged by calling TempData.Keep(key).
  3. RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.

您可以使用会话变量也只有主要问题是,会话变量都非常繁忙,<$比较C $ C>的TempData 。最后,你能够保持整个数据的控制器/地区的也。

You could use Session Variable also, Only major problem is that Session Variable are very heavy comparing with TempData. Finally you are able to keep the data across Controllers/Area also.

的TempData 工作在新的选项卡/视窗的也一样,会话变量一样。

TempData works in new Tabs/Windows also, like Session variable does.

您可以缓存数据某些变量,可以再次重复使用在了的TempData 做了同样的方式。

You can Cache the Data in some variable and can be reused again In the same manner done for TempData.

这篇关于如何在MVC 4基于用户过滤后的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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