实例化一个辅助类,但不是在视图 [英] Instantiate a helper class but not in view

查看:136
本文介绍了实例化一个辅助类,但不是在视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来实例化应该帮助我做一些查询字符串的建设,当涉及到我的视图链接的类多数民众赞成,我不能用这个方法,我需要为静态方法,因为这会导致querystringbuilder到在应用程序(这会导致一些严重的问题)

I need a way to instantiate a class thats supposed to help me with some querystring-building when it comes to my links in the view, and I cant use this methods that i require as static methods since that would cause the querystringbuilder to keep data during the whole life-time of the application (which would cause some serious problems)

所以,我给你们的问题是,
是否有可能一些如何能够实例类/对象,我需要,但不是在实际视图本身?

So my question to you guys is, Is it possible to some how be able to instantiate the class/object that i require but not in the actual view itself?

所以要保持这个问题简单..反正是有,我可以做这样的事情:

SO to keep the question simple.. is there anyway that I could do something like:

@MyInstantiatedObject.DoStuff()

在我的了,我认为这样做之前这样的观点:

in my view with out doing this before in my view:

@{
    var MyInstantiatedObject = new MyClass()

}

我得到我一些地方一些如何将需要instansiate的对象,但我的问题是,如果可能做到这一点以其他方式(如告诉web.config中使用一些APP_ $到韩德尔it..or C $ç@helper魔法什么的)

I do get that I some where some how will need to instansiate the object, but my question is if its possible to do it in some other manner (like telling the web.config to handel it..or using some app_code @helper magic or something)

在此先感谢!

推荐答案

你所要实现的目标违背了MVC的理念。如果你想保持动作之间的查询字符串数据,你可以创建自定义的ActionLink的HTML帮助是这样的:

What you are trying to achieve goes against the philosophy of MVC. If you want to keep the query string data between the actions, you can create your custom ActionLink html helper like this:

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
    string linkText, string actionName)
{
    var routeValueDictionary = new RouteValueDictionary();

    return htmlHelper.ActionLinkWithQueryString(linkText, 
        actionName, routeValueDictionary);
}

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
   string linkText, string actionName, RouteValueDictionary routeValues)
{
    var queryString = HttpContext.Current.Request.QueryString;

    if (queryString.Count > 0)
    {
        foreach (string key in queryString.Keys)
        {
            routeValues.Add(key, queryString[key]);
        }
    }

    return htmlHelper.ActionLink(linkText, actionName, routeValues);
}

您也可以在控制器或在控制器延伸像这样创建自定义RedirectToAction方法:

You can also create a custom RedirectToAction method in your Controller or in a Controller Extention like this:

private RedirectToRouteResult RedirectToActionWithQueryString(string actionName)
{
    var queryString = Request.QueryString;
    var routeValues = new RouteValueDictionary();

    foreach (string key in queryString.Keys)
    {
        routeValues.Add(key, queryString[key]);
    }

    return RedirectToAction(actionName, routeValues);
}

这篇关于实例化一个辅助类,但不是在视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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