Geeks With Blogs
Zackary Geers blog

I'm working on a existing project, where we've recently added in MVC, but there's a lot of existing Asp.Net code.  I wanted MVC to generate the link for me instead of hard coding it.  Here's how I did it.  I started by creating a class that implments IViewDataController, so I can construct the helper I need.  Then I can new up the HtmlHelper, set it's Context, and I have access to all of the MVC goodness inside of my old page.

    public class SimpleViewDataContainer : IViewDataContainer
    {
        public SimpleViewDataContainer()
        {
            ViewData = new ViewDataDictionary();
        }
        public ViewDataDictionary ViewData { get; set; }
    }

    HtmlHelper helper = new HtmlHelper(new ViewContext(), new SimpleViewDataContainer());
    helper.ViewContext.HttpContext = new HttpContextWrapper(HttpContext.Current);
    int id = int.Parse(Request["somethingFromQueryString"]);
    Response.Redirect(helper.BuildUrlFromExpression<MyController>(x => x.MyAction(id)));

Posted on Wednesday, January 13, 2010 9:41 PM | Back to top


Comments on this post: MVC Helpers inside of Old Pages

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Zackary Geers | Powered by: GeeksWithBlogs.net