2

I want to pass htmlAttributes as parameter to my HtmlHelper similar as it created in Html.ActionLink("linktext", "Home", null, new{width="100px"}) How to pass this new{width="100px"} to my method

    public static string SelectCategoryAdminWithAllItem(this HtmlHelper htmlHelper, string name, **???**)
    { }

and how to parse it?

Thanks

2 Answers 2

5

Always try to look at sources when interested with this kind of questions. From the implementation of HtmlHelper.TextBox

public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
{
    return htmlHelper.TextBox(name, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

as you see, type of parameter is object as you cant use anonymous types as parameters to methods, and object is choice. And when parsing it, you can use HtmlHelper.AnonymousObjectToHtmlAttributes Method

Sign up to request clarification or add additional context in comments.

1 Comment

I use ASP.NET MVC 2.0, not 3.0 :(
3

I looked through the source for MVC2 when trying to figure this one out. In MVC2 they used an overload of RouteValueDictionary in System.Web.Routing to turn an object to a dictionary rather than having a helper method available like in MVC3.

public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
{
    return htmlHelper.TextBox(name, value, new RouteValueDictionary(htmlAttributes));
}

A bit counter intuitive but that's the standard in 2.

Edit: Updated tags to include mvc2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.