CoffeeCode
CoffeeCode

Reputation: 4314

How To Call The RenderAction in my Html Helper?

im extending the htmlhelper. but it i cannot call the renderaction of it.

 using System.Text;
 using System.Web.Mvc;
 using System.Web.Mvc.Html;
 public static class ViewHelpers
    {
        public static string Text(this HtmlHelper htmlHelper, string name, object value, bool isEditMode)
        {
           htmlHelper.RenderAction(...) //cannot be called
        }
    }

how can i call the RenderAction???

Upvotes: 2

Views: 2223

Answers (3)

sonjz
sonjz

Reputation: 5090

CoffeeCode was on the right track, but the namespace to include should be System.Web, System.Web.Mvc and System.Web.Mvc.Html.

using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Resources;
using System.Web.Routing;
using System.Text.RegularExpressions;

namespace xxx.yyy.Helpers {
    public static class HelperView {
        public static void RenderHeader(this HtmlHelper helper) {
            helper.RenderAction("Header", "Default", new { pageAction = helper.ViewContext.RouteData.Values["action"].ToString() });
        }
    }
}

http://msdn.microsoft.com/en-us/library/ee703490(v=vs.108).aspx

Upvotes: 3

CoffeeCode
CoffeeCode

Reputation: 4314

the solution was realy easy.
add using Microsoft.Web.Mvc;
and when calling the RenderAction it will write the html string directly to the view

Upvotes: -2

griegs
griegs

Reputation: 22760

@CoffeeCode, could you explain why you'd like to do this?

Generally speaking you would call a helper to return html as a string which is then rendered in the page.

It seems a little weird to want to render it within the helper. At least to me anyway.

edit

i think a helper here might be the wrong thing for you. if you want access to the list, but you don't want to pass it to the view then a helper is the wrong place. You may want to create a class that takes the list, does stuff with it and then return html.

Upvotes: 2

Related Questions